Why is my GAN collapsing each time I run it? - generative-adversarial-network
So my GAN is producing the same output every time I run it and there are also repeating outputs.
The GAN always produces the same faces every time I run it and I can't figure out what is causing this issue. I want each output to be unique but many are repeating. It might be mode collapse, but I have yet to figure it out after looking over my code for a while and trying to rewrite some of it.
random_dim = 100
PREVIEW_ROWS = 6
PREVIEW_COLS = 6
PREVIEW_MARGIN = 0
SAVE_FREQ = 10
IMAGE_SIZE = 64
WIDTH = 64
HEIGHT = 64
CONTROL_SIZE_SQRT = 6
CHANNELS = 3
EPOCHS = 30000
def get_optimizer():
return Adam(lr=0.0002, beta_1=0.5, beta_2=0.999)
def get_generator():
gen_input = Input(shape=random_dim)
generator = Sequential()
generator.add(Dense(512 * 2 * 2, input_dim=random_dim))
generator.add(LeakyReLU(alpha=0.2))
generator.add(Reshape((2, 2, 512)))
generator.add(Conv2DTranspose(64, (3, 3), strides=(2,2), padding='same'))
generator.add(LeakyReLU(alpha=0.2))
generator.add(BatchNormalization())
generator.add(Conv2DTranspose(128, (3, 3), strides=(2,2), padding='same'))
generator.add(LeakyReLU(alpha=0.2))
generator.add(BatchNormalization())
generator.add(Conv2DTranspose(256, (3, 3), strides=(2,2), padding='same'))
generator.add(LeakyReLU(alpha=0.2))
generator.add(BatchNormalization())
generator.add(Conv2DTranspose(512, (3, 3), strides=(2,2), padding='same'))
generator.add(LeakyReLU(alpha=0.2))
generator.add(BatchNormalization())
generator.add(Conv2DTranspose(512, (3, 3), strides=(2,2), padding='same'))
generator.add(LeakyReLU(alpha=0.2))
generator.add(BatchNormalization())
generator.add(Conv2D(3, (3,3), activation='tanh', padding='same'))
input = Input(shape=(random_dim,))
generated_image = generator(input)
generator.summary()
return Model(input, generated_image)
def get_discriminator():
disc_input = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3))
discriminator = Sequential()
discriminator.add(Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3)))
discriminator.add(GaussianNoise(0.1))
discriminator.add(Conv2D(64, (3, 3), padding='same'))
discriminator.add(LeakyReLU(alpha=0.2))
discriminator.add(GaussianNoise(0.1))
discriminator.add(AveragePooling2D())
discriminator.add(Conv2D(128, (3, 3), padding='same'))
discriminator.add(LeakyReLU(alpha=0.2))
discriminator.add(GaussianNoise(0.1))
discriminator.add(AveragePooling2D())
discriminator.add(Conv2D(128, (3, 3), padding='same'))
discriminator.add(LeakyReLU(alpha=0.2))
discriminator.add(GaussianNoise(0.1))
discriminator.add(AveragePooling2D())
discriminator.add(Conv2D(256, (3, 3), padding='same'))
discriminator.add(LeakyReLU(alpha=0.2))
discriminator.add(GaussianNoise(0.1))
discriminator.add(AveragePooling2D())
discriminator.add(Conv2D(512, (3, 3), padding='same'))
discriminator.add(LeakyReLU(alpha=0.2))
discriminator.add(GaussianNoise(0.1))
discriminator.add(AveragePooling2D())
discriminator.add(Flatten())
discriminator.add(LeakyReLU(alpha=0.2))
discriminator.add(Dropout(0.5))
discriminator.add(Dense(1, activation='sigmoid'))
discriminator.summary()
discriminator = Model(disc_input, discriminator(disc_input))
optimizer = RMSprop(
lr = .0001,
clipvalue = 1.0,
decay = 1e-8
)
discriminator.compile(loss='binary_crossentropy', optimizer=get_optimizer(), metrics=["accuracy"])
return discriminator
def get_gan_network(discriminator, random_dim, generator, optimizer):
# We initially set trainable to False since we only want to train either the
# generator or discriminator at a time
discriminator.trainable = False
# gan input (noise) will be 100-dimensional vectors
gan_input = Input(shape=(random_dim,))
# the output of the generator (an image)
x = generator(gan_input)
# get the output of the discriminator (probability if the image is real or not)
gan_output = discriminator(x)
gan = Model(inputs=gan_input, outputs=gan_output)
gan.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=["accuracy"])
return gan
X_train = training_data
fixed_noise = np.random.normal(0, 1, (PREVIEW_ROWS * PREVIEW_COLS, 100))
def train(epochs=1, batchSize=128):
batchCount = X_train.shape[0] / batchSize
halfBatch = batchSize // 2
print(X_train.shape[0])
print('Epochs:', epochs)
print('Batch size:', batchSize)
print('Batches per epoch:', batchCount)
adam = get_optimizer()
generator = get_generator()
discriminator = get_discriminator()
discriminator.trainable = False
gan = get_gan_network(discriminator, random_dim, generator, adam)
valid = np.ones((batchSize, 1))
fake = np.zeros((batchSize, 1))
d_losses = []
g_losses = []
real_scores = []
start=0
for epoch in tdqm(range(EPOCHS)):
start_time = time.time()
latent_vectors = np.random.normal(size=(batchSize, random_dim))
generated = generator.predict(latent_vectors)
gen_shape = generator.output_shape
real = training_data[start:start + batchSize]
combined_images = np.concatenate([generated, real])
labels = np.concatenate([np.ones((batchSize, 1)), np.zeros((batchSize, 1))])
labels += .05 * np.random.random(labels.shape)
d_loss = discriminator.train_on_batch(combined_images, labels)
d_losses.append(d_loss)
#Train G
latent_vectors = np.random.normal(size=(batchSize, random_dim))
misleading_targets = np.zeros((batchSize, 1))
g_loss = gan.train_on_batch(latent_vectors, misleading_targets)
g_losses.append(g_loss)
#if epoch == 1 or epoch % 20 == 0:
if epoch % 50 == 49:
save_images(epoch, fixed_noise, generator)
if __name__ == '__main__':
train(5000, 32)
GAN is very volatile. It collapses everytime if proper training and good hyperparameter tuning is not done (even with small datasets like MNIST). There are many new advancements in GAN networks that helps in avoiding mode collapse (Ex: AdaGAN).
But as you're training a Vanilla GAN (the basic one), to avoid collapse, follow the hacks mentioned in the given Repository.
These tricks will avoid Mode collapse upto a maximum extent
Related
Cv2 findChessboardCorners fails to find corners on the images
I am trying to calibrate the camera with 10-50mm focal lenght, all the images of the chess board are taken with cube size as 0.25cm. when i run the findchessboard function of cv2, it fails to detect the chessboard. image file = "filename" img = cv2.imread(file) # Color-segmentation to get binary mask lwr = np.array([0, 0, 90]) upr = np.array([179, 61, 252]) hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) msk = cv2.inRange(hsv, lwr, upr) plt.imshow(msk) plt.show() # Extract chess-board krn = cv2.getStructuringElement(cv2.MORPH_RECT, (50, 30)) dlt = cv2.dilate(msk, krn, iterations=5) res = 255 - cv2.bitwise_and(dlt, msk) # Displaying chess-board features res = np.uint8(res) plt.imshow(res) plt.show() ret, corners = cv2.findChessboardCorners(gray, (9, 7), flags=cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGE + cv2.CALIB_CB_EXHAUSTIVE) if ret: print(corners) fnl = cv2.drawChessboardCorners(img, (7, 7), corners, ret) plt.imshow(fnl) plt.show else: print("No Checkerboard Found")
Issues Running the updated version of grts() within the package spsurvey
With the updated version of spsurvey I am having a difficult time trying to generate random points for my design using the grts() function. With the code below I can produce points if n_over = some number and it draws that number across all strata. However I would like a different number of oversamples per stratum based on acres. The first bit of code works and draws the right amount of base points then produces 10 over sample points per stratum However when I try to do something similar for n_over (Over.test) I receive this error message " Input Error Message n_base + n_over : For each stratum, the sum of the base sites and 'Over' replacement sites must be no larger than the number of rows in 'sframe' representing that stratum." Even though there are 115+ rows per strata in the test.inShape<-st_read("UFO_2022_Ints_Pts_Strata.shp") There is unfortunately very little information out on the updated spsurvey package. "ALLOT_NAME" is a column within the shape file that contains the name for each stratum (Adobe, Big Pasture, ect...) New to Stackoverflow so I apologize if I have not presented this appropriately ### Working code #### test.prj=c("UFO_2022_Ints_Pts_Strata") test.inShape<-st_read("UFO_2022_Ints_Pts_Strata.shp") plots.df<-read.csv("GUSG_Strata.csv") strata.list<-as.vector(plots.df) x<-c('Adobe' = 2, 'Big Pasture' = 2, 'Black Ridge' = 45, 'Blue Cimarron' = 2, Buck = 2, 'Crawford Reservoir' = 2, 'Dave Wood Road' = 2, 'Dry Cedar' = 2, 'East Gould Reservoir' = 2, 'Gould Reservoir' = 20, 'Green Mountain - Middle' = 35, 'Grizzly Gulch' = 2, 'Iron Canyon' = 15, 'Little Baldy' = 2, 'Lower Horsefly' = 35, 'Onion Valley' = 2, 'Poison Spring' = 15, 'Rawhide - Coffee Pot' = 2, 'Rim Rock' = 2, 'Shinn Park' = 2, 'Tappan Creek' = 2, 'Green Mountain - Jensen Ware' = 15, 'Green Mountain - West' = 35, 'Green Mountain - East' = 30) #test.sample<-grts(design=test.design, DesignID="RGFO_2022_RangeLPI", in.shape=test.inShape, id = "RGFO_2022_RangeLPI", prjfilename=test.prj, out.shape="RGFO_2022_RangeLPI_GRTS") test.sample<-grts(test.inShape, n_base=x, stratum_var="ALLOT_NAME", n_over=10, DesignID="UFO_2022_GUSG" ) test.output<-sp_rbind(test.sample) st_write(test.output, "UFO_2022_GUSG_SampleDesign_V1_TESTErase.shp") # will not work when I use n_over = Over.list trying to specify oversample points for each strata Over.list <- c('Adobe' = 3, 'Big Pasture' = 3, 'Black Ridge' = 10, 'Blue Cimarron' = 3, 'Buck' = 3, 'Crawford Reservoir' = 3, 'Dave Wood Road' = 3, 'Dry Cedar' = 3, 'East Gould Reservoir' = 3, 'Gould Reservoir' = 3, 'Green Mountain - Middle' = 10, 'Grizzly Gulch' = 3, 'Iron Canyon' = 10, 'Little Baldy' = 3, 'Lower Horsefly' = 10, 'Onion Valley' = 3, 'Poison Spring' = 10, 'Rawhide - CoffeePot' = 3, 'Rim Rock' = 3, 'Shinn Park' = 3, 'Tappan Creek' = 3, 'Green Mountain - Jensen Ware' = 10, 'Green Mountain - West' = 10, 'Green Mountain - East' = 10) ### Below is the layout of the grts function # grts # sframe, # n_base, # stratum_var = NULL, # seltype = NULL, # caty_var = NULL, # caty_n = NULL, # aux_var = NULL, # legacy_var = NULL, # legacy_sites = NULL, # legacy_stratum_var = NULL, # legacy_caty_var = NULL, # legacy_aux_var = NULL, # mindis = NULL, # maxtry = 10, # n_over = NULL, # n_near = NULL, # wgt_units = NULL, # pt_density = NULL, # DesignID = "Site", # SiteBegin = 1, # sep = "-", # projcrs_check = TRUE # )
Sorry for the late response here! I think the error you are receiving is the result of a bug in one of our error checks. This bug will be fixed in version 5.4.0 of spsurvey, which should be on CRAN within the next couple weeks. I am hopeful your code will work once the new version is installed. Additionally, spsurvey has a new website viewable here that contains a lot of information about the package. As for "presenting appropriately" on Stack Overflow, usually you want to provide a reproducible example (more on that here and here). Given the nature of this bug, I think that providing a reproducible example would have been challenging. Thus, I appreciate that you provided all of your code. EDIT: spsurvey version 5.4.0 was pushed to CRAN on November 22, 2022.
Error while running WTC (Wavelet Coherence) Codes in R
I am doing Wavelet Analysis in R using Biwavelet. However, I receive the error message: Error in check.datum(y) : The step size must be constant (see approx function to interpolate) When I run the following code: wtc.AB = wtc(t1, t2, nrands = nrands) Please share your help here. Complete Code is: # Import your data Data <- read.csv("https://dl.dropboxusercontent.com/u/18255955/Tutorials/Commodities.csv") # Attach your data so that you can access variables directly using their # names attach(Data) # Define two sets of variables with time stamps t1 = cbind(DATE, ISLX) t2 = cbind(DATE, GOLD) # Specify the number of iterations. The more, the better (>1000). For the # purpose of this tutorial, we just set it = 10 nrands = 10 wtc.AB = wtc(t1, t2, nrands = nrands) # Plotting a graph par(oma = c(0, 0, 0, 1), mar = c(5, 4, 5, 5) + 0.1) plot(wtc.AB, plot.phase = TRUE, lty.coi = 1, col.coi = "grey", lwd.coi = 2, lwd.sig = 2, arrow.lwd = 0.03, arrow.len = 0.12, ylab = "Scale", xlab = "Period", plot.cb = TRUE, main = "Wavelet Coherence: A vs B")```
R circlize chordDiagram how to improve image qualiity
I am attempting to make a chord diagram of a fairly large 18*65 table (not every cell has a value). I have generated the image I want but the quallity of it is nothing like what is shown on the github seen below: I figure maybe the number of cells needing to be plotted may cause problems but otherwise I am not sure why i get such a difference: circos.par(gap.after = c(rep(2, ncol(chord_data)-1), 10, rep(2, 8-1), 5, rep(2, 10-1), 5, rep(2, 5-1), 5, rep(2, 3-1), 5, rep(2, 1), 5, rep(2, 12-1), 5, rep(2, 10-1), 5, rep(2, 6-1), 5, rep(2, 7-1), rep(2, 3-1), 10)) png(file = "antismash_by_type.png", width = 800, height = 800) chordDiagram(chord_data, grid.col = grid.col, order = order, annotationTrack = "grid", preAllocateTracks = 1) circos.track(track.index = 1, panel.fun = function(x, y) { circos.text(CELL_META$xcenter, CELL_META$ylim[1], CELL_META$sector.index, facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5)) }, bg.border = NA) dev.off() Secondly my chords do not appear to be scaling to the value of the cell which ranges from 0-100 and from what I read this is meant to occure by default but does not appear to be.
Parts Missing From Plot, That then Reappear and Overwrite The Entire Plot When Saved? (R, Heatmap.2)
I'm using heatmap.2 to create a plot, however, the initial plot that is saved to my source folder is missing a key and title. When I then run the dev.off() command, the Key and the Title are then used to overwrite the original graph? For instance, I will produce a plot like this: Which is far from perfect. But then when I run the dev.off() to close the device (otherwise a host of other errors ensue): What you are looking at above is a very distorted Key and my 'XYZ' title. Why on earth is it creating two files, firstly the one with my matrix, and then overwriting this with a second file containing my flipping key and my title? I cannot follow the logic. I've updated my OS, my version of R, RStudio, all my packages and unistalled RStudio. Nothing seems to help. If you'd like to try and replicate my error here is the example matrix: structure(c(1, 4, 5, 3, 3, 4, 6, 1, 7, 5, 5, 4, 4, 8, 1, 3, 9, 2, 2, 9, 3, 1, 3, 4, 4, 5, 5, 5, 1, 4, 4, 3, 3, 3, 9, 1), .Dim = c(6L, 6L)) And this is the script I'm using to plot my example data. You'll need to provide a SourceDir and make sure you assign the matrix to the name "Matrix". if (!require("gplots")) { install.packages("gplots", dependencies = TRUE) library(gplots) } if (!require("RColorBrewer")) { install.packages("RColorBrewer", dependencies = TRUE) library(RColorBrewer) } my_palette <- colorRampPalette(c("snow", "yellow", "darkorange", "red"))(n = 399) transition col_breaks = c(seq(0,1,length=100), #white 'snow' seq(2,4,length=100), # for yellow seq(5,7,length=100), # for orange 'darkorange' seq(8,9,length=100)) # for red png(paste(SourceDir, "Heatmap_Test.png"), width = 5*1000, height = 5*1000, res = 300, pointsize =15) heatmap.2(Matrix, main = paste("XYZ"), notecol="black", key = "true" , colsep = c(3, 6, 9), rowsep = c(3, 6, 9), labCol = NULL, labRow = NULL, sepcolor="white", sepwidth=c(0.08,0.08), density.info="none", trace="none", margins=c(1,1), col=my_palette, breaks=col_breaks, dendrogram="none", RowSideColors = c(rep("blue", 3), rep("orange", 3)), ColSideColors = c(rep("blue", 3), rep("orange", 3)), srtCol = 0 , asp = 1 , adjCol = c(NA, 0) , adjRow = c(0, NA) , #keysize = 2 , Colv = FALSE , Rowv = FALSE , key.xlab = paste("Correlation") , cexRow = (1.8) , cexCol = (1.8) , notecex = (1.5) , lmat = rbind(c(0,0,0,0), c(0,0,2,0),c(0,1,3,0),c(0,0,0,0)) , #par(ColSideColors = c(2,2)), lhei = c(1, 1, 3, 1) , lwid = c(1, 1, 3, 1)) dev.off() I'd really appreciate any insight into this problem.
I believe this resulted from the fact that I had more than just elements 1 to four, as the coloured rows I had added counted as additional elements that had to be arranged in the display matrix. As such: mat = rbind(c(0,0,0,0), c(0,0,2,0),c(0,1,3,0),c(0,0,0,0)) , lhei = c(1, 1, 3, 1) , lwid = c(1, 1, 3, 1)) No longer cut the butter. After much ado, I finally managed to get the following layout to work (on my actual data, not my example data). lmat = rbind(c(0,4,5,0), c(0,0,2,0),c(0,1,3,0),c(0,0,6,0)) , lhei = c(0.4, 0.16, 3, 0.4) , # Alter dimensions of display array cell heighs lwid = c(0.4, 0.16, 3, 0.4), Notice the inclusion of elements 5 and 6. So my final command looks like this (note that there will be many other changes but the real progress happened once I added in 5 and 6): png(paste(SourceDir, "XYZ.png"), width = 5*1500, height = 5*1500, res = 300, # 300 pixels per inch pointsize =30) heatmap.2(CombinedMtx, main = paste("XYZ"), # heat map title notecol="black", key = "true" ,# change font color of cell labels to black colsep = c(6, 12, 18), labCol = c(" "," "," ", "XX"," "," "," "," "," ", "YY"," "," "," "," "," ", "ZZ"," "," "," "," "," ", "QQ"), rowsep = c(6, 12, 18), labRow = c(" "," "," ", "XX"," "," "," "," "," ", "YY"," "," "," "," "," ", "ZZ"," "," "," "," "," ", "QQ"), sepcolor="white", sepwidth=c(0.08,0.08), density.info="none", trace="none", margins=c(1,1), col=my_palette, breaks=col_breaks, dendrogram="none", RowSideColors = c(rep("#deebf7", 6), rep("#1c9099", 6), rep("#addd8e", 6), rep("#fee391", 6)), ColSideColors = c(rep("#deebf7", 6), rep("#1c9099", 6), rep("#addd8e", 6), rep("#fee391", 6)), srtCol = 0 , asp = 1 , adjCol = c(1.5, -61.5) , adjRow = c(0, -1.38), offsetRow = (-59.5), keysize = 2 , Colv = FALSE , Rowv = FALSE , key.xlab = NA , key.ylab = NULL , key.title = NA , cexRow = (1.6) , cexCol = (1.6) , notecex = (1.5) , cex.main = (20), lmat = rbind(c(0,4,5,0), c(0,0,2,0),c(0,1,3,0),c(0,0,6,0)) , #par(ColSideColors = c(2,2)), lhei = c(0.4, 0.16, 3, 0.4) , # Alter dimensions of display array cell heighs lwid = c(0.4, 0.16, 3, 0.4), symkey = any(0.5 < 0, na.rm=FALSE) || col_breaks, key.par=list(mar=c(3.5,0, 1.8,0) ) #tweak specific key paramters ) dev.off() Also, if you don't start each time by creating the PNG and enf each time by using dev.off() it won't work. I believe this might also have been contribution to my confusion, and potentially after drawing the heatmap, some elements were being drawn once the dev.off() command was run, causing the heatmap to be overwritten. This (with my matrix) creates this image. What I have done is a really gammy way of labelling my blocks but until I can work out how to get ComplexHeatmap working properly I'll be stuck using hacks like this with Heatmap.2.