Question: My code is not working it's throwing error ' ' ' from numpy import expand _ dims from numpy import zeros from numpy import ones
My code is not working it's throwing error
from numpy import expanddims
from numpy import zeros
from numpy import ones
from numpy import vstack
from numpy.random import randn
from numpy.random import randint
from keras.optimizers import Adam
from keras.models import Sequential
from keras.layers import Dense,Reshape,Flatten,ConvDConvDTranspose,LeakyReLU,Dropout
from matplotlib import pyplot
import numpy as np
import cv
# function to generate discriminator model
def definediscriminatorinshape:
model Sequential
# normal
model.addConvD padding'same', inputshapeinshape
model.addLeakyReLUalpha
# downsample
model.addConvD strides padding'same'
model.addLeakyReLUalpha
# downsample
model.addConvD strides padding'same'
model.addLeakyReLUalpha
# downsample
model.addConvD strides padding'same'
model.addLeakyReLUalpha
# classifier
model.addFlatten
model.addDropout
model.addDense activation'sigmoid'
# compile model
opt Adamlr beta
model.compileloss'binarycrossentropy', optimizeropt, metricsaccuracy
return model
# function to generate standalone generator model
def definegeneratorlatentdim:
model Sequential
# foundation for x image
nnodes
model.addDensennodes, inputdimlatentdim
model.addLeakyReLUalpha
model.addReshape
# upsample to x
model.addConvDTranspose strides padding'same'
model.addLeakyReLUalpha
# upsample to x
model.addConvDTranspose strides padding'same'
model.addLeakyReLUalpha
# upsample to x
model.addConvDTranspose strides padding'same'
model.addLeakyReLUalpha
# output layer
model.addConvD activation'tanh', padding'same'
return model
# define the combined generator and discriminator model, for updating the generator
def definegangmodel, dmodel:
# make weights in the discriminator not trainable
dmodel.trainable False
# connect them
model Sequential
# add generator
model.addgmodel
# add the discriminator
model.adddmodel
# compile model
opt Adamlr beta
model.compileloss'binarycrossentropy', optimizeropt
return model
# load and prepare cifar training images
def loadrealsamples:
# load cifar dataset
X nploadimgdata.txtnpy
# convert from unsigned ints to floats
#X trainX.astypefloat
# scale from to
#X X
return X
# select real samples
def generaterealsamplesdataset nsamples:
# choose random instances
ix randint dataset.shape nsamples
# retrieve selected images
X datasetix
# generate 'real' class labels
y onesnsamples,
return X y
# generate points in latent space as input for the generator
def generatelatentpointslatentdim, nsamples:
# generate points in the latent space
xinput randnlatentdim nsamples
# reshape into a batch of inputs for the network
xinput xinput.reshapensamples, latentdim
return xinput
# use the generator to generate n fake examples, with class labels
def generatefakesamplesgmodel, latentdim, nsamples:
# generate points in latent space
xinput generatelatentpointslatentdim, nsamples
# predict outputs
X gmodel.predictxinput
# create 'fake' class labels
y zerosnsamples,
return X y
# create and save a plot of generated images
def saveplotexamples epoch, n:
# scale from to
examples examples
# plot images
for i in rangen n:
# define subplot
pyplot.subplotn n i
# turn off axis
pyplot.axisoff
# plot raw pixel data
pyplot.imshowexamplesi
cvimwriteimgstrijpgexamplesi
# save plot to file
filename 'modelgeneratedplotedpngepoch
pyplot.savefigfilename
pyplot.close
# evaluate the discriminator, plot generated images, save generator model
def summarizeperformanceepoch gmodel, dmodel, dataset, latentdim, nsamples:
# prepare real samples
Xreal, yreal generaterealsamplesdataset nsamples
# evaluate discriminator on real examples
accreal dmodel.evaluateXreal, yreal, verbose
# prepare fake examples
xfake, yfake generatefakesamplesgmodel, latentdim, nsamples
# evaluate discriminator on fake examples
accfake dmodel.evaluatexfake, yfake, verbose
# summarize discriminator performance
printAccuracy real: f fake: faccreal accfake
# save plot
saveplotxfake, epoch
# save the generator model tile file
filename 'modelgeneratormodeldhepoch
gmodel.savefilename
traingmodel, dmodel, ganmodel, dataset, latentdim
please help and help cloudeploy
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
