def train_model(self): """ Trainer: Trains the loaded autoencoder model :param epochs: number of epochs run :param batch_size: how many imgs in each batch :param save_interval: how many epochs between each save """ if self.info==None: print("Warning no info found, prompting for info") self.set_training_info() globals().update(self.info) if self.model==None: print("Error: no model loaded") return if self.pretrained==True: print("Warning: model has pretrained weights") from tqdm import tqdm y1,y2,x1,x2=cutter.find_square_coords(plotload.load_polyp_batch(self.img_shape, batch_size)) for epoch in tqdm(range(epochs)): X_train=plotload.load_polyp_batch(self.img_shape, batch_size,data_type='med/none') if mask==0: Y_train,X_train=cutter.add_green_suare(X_train) else: print("Not yet implimented") cur_loss=self.model.train_on_batch(X_train, Y_train) if epoch%10==0: self.save_img(epoch) if cur_loss<self.threshold: print(cur_loss) self.threshold=cur_loss self.model.save(f"models/AE-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if mask==0 else 'n'}.h5") self.model.save_weights(f"models/AE-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if mask==0 else 'n'}-w.h5") self.model.save(f"models/AE-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if mask==0 else 'n'}-fin.h5")
def __init__(self, img_cols, img_rows): """ Initializes the autoencoder. """ self.set_training_info() globals().update(self.info) self.threshold = threshold self.img_cols = img_cols # Original is ~576 self.img_rows = img_rows # Original is ~720 self.channels = 3 # RGB self.img_shape = (self.img_cols, self.img_rows, self.channels) if not mask: dummy = plotload.load_polyp_batch(self.img_shape, 20, data_type='med/stool-inclusions', crop=False) self.dims = cutter.find_square_coords(dummy) self.combined = None self.discriminator = None self.generator = None self.pretrained = False
def train_model(self): def t(m, bol): for layer in m.layers: layer.trainable = bol if self.info == None: print("Warning no info found, prompting for info") self.set_training_info() globals().update(self.info) if self.combined == None: print("Error: no model loaded") return if self.pretrained == True: print("Warning: model has pretrained weights") half_batch = int(batch_size / 2) for epoch in tqdm(range(epochs)): # --------------------- # Train Discriminator # --------------------- X_train = plotload.load_polyp_batch(self.img_shape, batch_size, data_type='none', crop=False) idx = np.random.randint(0, X_train.shape[0], half_batch) imgs = X_train[idx] if corner: masked_imgs, missing, _ = ms.mask_green_corner(imgs) else: masked_imgs, missing, _ = ms.mask_randomly_square( imgs, self.mask_height, self.mask_width) # Generate a half batch of new images gen_missing = self.generator.predict(masked_imgs) if soft: valid = 0.2 * np.random.random_sample((half_batch, 1)) + 0.9 fake = 0.1 * np.random.random_sample((half_batch, 1)) else: valid = np.ones((half_batch, 1)) fake = np.zeros((half_batch, 1)) if epoch % 120 == 0: #small shakeup to get out of local minimas placeholder = valid valid = fake fake = placeholder # Train the discriminator d_loss_real = self.discriminator.train_on_batch(missing, valid) d_loss_fake = self.discriminator.train_on_batch(gen_missing, fake) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) # --------------------- # Train Generator # --------------------- # Select a random half batch of images idx = np.random.randint(0, X_train.shape[0], batch_size) imgs = X_train[idx] if corner: masked_imgs, missing_parts, _ = ms.mask_green_corner(imgs, val=-1) else: masked_imgs, missing_parts, _ = ms.mask_randomly_square( imgs, self.mask_height, self.mask_width) # Generator wants the discriminator to label the generated images as valid valid = np.ones((batch_size, 1)) # Train the generator t(self.discriminator, False) g_loss = self.combined.train_on_batch(masked_imgs, [missing_parts, valid]) t(self.discriminator, True) # Plot the progress if epoch % 10 == 0: print( "%d [D loss: %f, acc: %.2f%%] [G loss: %f, mse: %f]" % (epoch, d_loss[0], 100 * d_loss[1], g_loss[0], g_loss[1])) self.save_img(epoch) if g_loss[1] < self.threshold: self.threshold = g_loss[1] self.generator.save( f"models/CE-gen-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}.h5" ) self.discriminator.save( f"models/CE-dic-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}.h5" ) self.combined.save( f"models/CE-com-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}.h5" ) self.combined.save_weights( f"models/CE-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}-w.h5" )
def train(self): from tqdm import tqdm X_train = {} Y_train = {} X_val = {} Y_val = {} X_test = {} Y_test = {} if True: polyps = plotload.load_polyp_batch(self.img_shape, 1000, data_type='polyps', crop=True, rot=True) polyps = np.concatenate( (polyps[0::4], polyps[1::4], polyps[2::4], polyps[3::4])) nonpolyps = plotload.load_polyp_batch(self.img_shape, 1000, data_type='normal-cecum', crop=True, rot=True) nonpolyps = np.concatenate((nonpolyps[0::4], nonpolyps[1::4], nonpolyps[2::4], nonpolyps[3::4])) np.save('polyps.npy', polyps) np.save('nonpolyps.npy', nonpolyps) else: polyps = np.load('polyps.npy') nonpolyps = np.load('nonpolyps.npy') totlen = len(polyps) // 4 trainlen = int(totlen * 0.8) vallen = int(totlen * 0.1) testlen = int(totlen * 0.1) polyp_set1 = polyps[:totlen] polyp_set2 = polyps[totlen:(2 * totlen)] polyp_set2 = polyp_set2[vallen + testlen:] polyp_set3 = polyps[(2 * totlen):(3 * totlen)] polyp_set3 = polyp_set3[vallen + testlen:] polyp_set4 = polyps[(3 * totlen):] polyp_set4 = polyp_set4[vallen + testlen:] nonpolyp_set1 = nonpolyps[:totlen] nonpolyp_set2 = nonpolyps[totlen:(2 * totlen)] nonpolyp_set2 = nonpolyp_set2[vallen + testlen:] nonpolyp_set3 = nonpolyps[(2 * totlen):(3 * totlen)] nonpolyp_set3 = nonpolyp_set3[vallen + testlen:] nonpolyp_set4 = nonpolyps[(3 * totlen):] nonpolyp_set4 = nonpolyp_set4[vallen + testlen:] X_test['polyp'] = polyp_set1[:testlen] X_test['nonpolyp'] = nonpolyp_set1[:testlen] X_val['polyp'] = polyp_set1[testlen:testlen + vallen] X_val['nonpolyp'] = nonpolyp_set1[testlen:testlen + vallen] X_train['polyp'] = np.concatenate( (polyp_set1[:trainlen], polyp_set2, polyp_set3, polyp_set4)) X_train['nonpolyp'] = np.concatenate( (nonpolyp_set1[:trainlen], nonpolyp_set2, nonpolyp_set3, nonpolyp_set4)) Y_train['polyp'] = np.ones(len(X_train['polyp'])).T Y_train['nonpolyp'] = np.zeros(len(X_train['polyp'])).T Y_val['polyp'] = np.ones(len(X_val['polyp'])).T Y_val['nonpolyp'] = np.zeros(len(X_val['polyp'])).T Y_test['polyp'] = np.ones(len(X_test['polyp'])).T Y_test['nonpolyp'] = np.zeros(len(X_test['polyp'])).T X_train['combined'] = np.concatenate( (X_train['polyp'], X_train['nonpolyp'])) Y_train['combined'] = np.concatenate( (Y_train['polyp'], Y_train['nonpolyp'])) X_val['combined'] = np.concatenate((X_val['polyp'], X_val['nonpolyp'])) Y_val['combined'] = np.concatenate((Y_val['polyp'], Y_val['nonpolyp'])) X_test['combined'] = np.concatenate( (X_test['polyp'], X_test['nonpolyp'])) Y_test['combined'] = np.concatenate( (Y_test['polyp'], Y_test['nonpolyp'])) if len(sys.argv) == 2: name = sys.argv[1] self.discriminator.fit( x=X_train['combined'], y=Y_train['combined'], epochs=10, validation_data=[X_val['combined'], Y_val['combined']], callbacks=[ TensorBoard(log_dir="./logs/" + name, histogram_freq=2, batch_size=32) ]) else: self.discriminator.fit( x=X_train['combined'], y=Y_train['combined'], epochs=10, validation_data=[X_val['combined'], Y_val['combined']]) #print(self.discriminator.evaluate(x=X_test['polyp'], y=Y_test['polyp'])) #print(self.discriminator.evaluate(x=X_test['nonpolyp'], y=Y_test['nonpolyp'])) guess = self.discriminator.predict(X_test['combined']) print((np.sum(np.rint(guess.T) == Y_test['combined'])) / len(guess)) print(np.rint(guess.T)) print(Y_test['combined']) for i, j in enumerate(guess): if np.rint(j) != Y_test['combined'][i]: print("guess=", j, " ans=", Y_test['combined'][i]) plt.imshow(X_test['combined'][i] * 0.5 + 0.5) plt.show()
#adding color dim mask_copy = np.expand_dims(mask_copy, -1) mask_copy = np.repeat(mask_copy, 3, axis=-1) for i, img in enumerate(org_copy): mask_copy[i] = np.multiply(mask_copy[i], gen_copy[i]) org_copy[i] = np.multiply(np.logical_not(mask_copy[i]), org_copy[i]) org_copy[i] = np.add(mask_copy[i], org_copy[i]) return org_copy if __name__ == '__main__': import plotload as pl import matplotlib.pyplot as plt img = pl.load_polyp_batch((576, 720, 3), 5, data_type="green", rot=False) #a,b,c=mask_randomly_square(img, 20, 100) a, b, c = mask_green_corner(img) plt.imshow(a[0] * 0.5 + 0.5) plt.show() plt.imshow(b[0] * 0.5 + 0.5) plt.show() #mask,missing,template=mask_from_template(img,rot=False) #plt.imshow(0.5*missing[0]+0.5) #plt.show() #plt.imshow(0.5*missing[0]+0.5) #plt.show() #img2=img[::-1,:,:,:] reconst = combine_imgs_with_mask(img2, img, template) #plt.imshow(0.5*reconst[0]+0.5) #plt.show()
def train_model(self): def t(m, bol): for layer in m.layers: layer.trainable = bol if self.info == None: print("Warning no info found, prompting for info") self.set_training_info() globals().update(self.info) if self.combined == None: print("Error: no model loaded") return if self.pretrained == True: print("Warning: model has pretrained weights") half_batch = batch_size for epoch in tqdm(range(epochs)): X_train = plotload.load_polyp_batch(self.img_shape, batch_size, data_type='med/none', crop=False) if corner: masked_imgs, missing, mask = ms.mask_green_corner(X_train) m = np.zeros(shape=X_train.shape) for i in range(X_train.shape[0]): m[i, mask[0]:mask[1], mask[2]:mask[3]] = missing[i] missing = m else: masked_imgs, missing, mask = ms.mask_from_template(X_train) if soft: valid = 0.2 * np.random.random_sample((half_batch, 1)) + 0.9 fake = 0.1 * np.random.random_sample((half_batch, 1)) else: valid = np.ones((half_batch, 1)) fake = np.zeros((half_batch, 1)) # --------------------- # Train Generator # --------------------- valid = np.ones((batch_size, 1)) # Train the generator t(self.discriminator, False) g_loss = self.combined.train_on_batch(masked_imgs, [X_train, valid]) t(self.discriminator, True) # --------------------- # Train discriminator # --------------------- gen_fake = self.generator.predict(masked_imgs) gen_fake = ms.combine_imgs_with_mask(gen_fake, X_train, mask) if epoch % 120 == 0 and epoch != 0: #small shakeup to get out of local minimas fake, valid = valid, fake # Train the discriminator d_loss_real = self.discriminator.train_on_batch(X_train, valid) d_loss_fake = self.discriminator.train_on_batch(gen_fake, fake) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) # Plot the progress print("[D: %f G: %f, mse: %f]" % (d_loss[0], g_loss[0], g_loss[1])) if g_loss[1] < self.threshold: self.threshold = g_loss[1] self.generator.save( f"models/CCgan-gen-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}.h5" ) self.discriminator.save( f"models/CCgan-dic-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}.h5" ) self.combined.save( f"models/CCgan-com-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}.h5" ) self.combined.save_weights( f"models/CCgan-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}-w-com.h5" ) self.discriminator.save_weights( f"models/CCgan-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}-w-dis.h5" ) self.generator.save_weights( f"models/CCgan-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}-w-gen.h5" ) if g_loss[1] < self.threshold: self.threshold = g_loss[1] self.generator.save( f"models/CCgan-gen-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}_fin.h5" ) self.discriminator.save( f"models/CCgan-dic-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}_fin.h5" ) self.combined.save( f"models/CCgan-com-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}_fin.h5" ) self.combined.save_weights( f"models/CCgan-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}-w-com_fin.h5" ) self.discriminator.save_weights( f"models/CCgan-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}-w-dis_fin.h5" ) self.generator.save_weights( f"models/CCgan-{self.img_shape[0]}-{self.img_shape[1]}-{'c' if corner else 'n'}-w-gen_fin.h5" )
output_img = input_img.copy() for i in range(img_count): output_img[i, y1:y2, x1:x2] = template[i] return input_img, output_img if __name__ == '__main__': import plotload import matplotlib.pyplot as plt #for i in range(10): # a=plotload.load_one_img((576,720), dest='green', crop=False, glare=False,printable=True) # _make_square_from_green_img(a) #_find_dims(a) b = plotload.load_polyp_batch((260, 260, 3), 10, data_type='green', crop=False) find_square_coords(b) c, d = add_green_suare(b) b = plotload.load_polyp_batch((260, 260, 3), 10, data_type='none', crop=False) c, d = add_green_suare(b) plt.imshow(c[0] * 0.5 + 0.5) plt.show() plt.imshow(d[0] * 0.5 + 0.5) plt.show()