def gui(adr=None, scale=1, img_rows=720, img_cols=576): """ gui is a full gui program that takes an image as input, and gives the user the option to make its own mask """ channels = 3 """ WARNING; here img shape is 720,576. this is trasposed of the rest of the program """ img_shape = (img_rows, img_cols, channels) img, img_path = pl.load_one_img((img_cols, img_rows, channels), dest=adr) scipy.misc.toimage(img, cmin=-1, cmax=1).save('tmp.png') #starting window, and adding buttons pygame.init() button0 = pygame.Rect(img_rows + 25, 0, 50, 50) button1 = pygame.Rect(img_rows + 25, 100, 50, 50) button2 = pygame.Rect(img_rows + 25, 200, 50, 50) button3 = pygame.Rect(img_rows + 25, 300, 50, 50) buttons = [button0, button1, button2, button3] global going going = True while going: img, img_path = pl.load_one_img(img_shape, dest='tmp.png') screen, px = _gui_setup('tmp.png', 1, buttons) mask_array, going = _gui_mainLoop(screen, px, buttons, img_shape=img_shape) scipy.misc.toimage(mask_array.T, cmin=-1, cmax=1).save('tmp.png') pygame.quit() return mask_array
def main(adr='2.jpg', scale=1, img_cols=576, img_rows=720): channels = 3 img_shape = (img_cols, img_rows, channels) generator = load_model("saved_model/generator.h5") generator.load_weights("saved_model/generator_weigths.h5") img, img_path = pl.load_one_img(img_shape, '1.jpg') scipy.misc.toimage(img, cmin=-1, cmax=1).save('working_img.png') global going going = True for i in range(3): mask = gui(adr='working_img.png').T #import matplotlib.pyplot as plt #plt.imshow(0.5*mask+0.5) #plt.show() np.save('/tmp/tmp_mask.npy', mask) img, img_path = pl.load_one_img(img_shape, "working_img.png") masked_imgs, missing_parts, m = mask_from_template(img, '/tmp', rot=False, fliplr=False, flipud=False) gen_fake = generator.predict(missing_parts) gen_fake = combine_imgs_with_mask(gen_fake, img, m) gen_fake = np.squeeze(gen_fake, axis=0) scipy.misc.toimage(gen_fake, cmin=-1, cmax=1).save('working_img.png')
def get_coord_live(adr='2.jpg', scale=1, img_rows=576, img_cols=720, mask_height=208, mask_width=280): """ Only used for contextencoder/live.py """ channels = 3 img_shape = (img_rows, img_cols, channels) missing_shape = (mask_width, mask_height, channels) from keras.models import load_model generator = load_model("saved_model/generator.h5") generator.load_weights("saved_model/generator_weigths.h5") import scipy.misc import plotload as pl import numpy as np img, img_path = pl.load_one_img(img_shape, dest=adr) scipy.misc.toimage(img, cmin=-1, cmax=1).save('tmp.png') pygame.init() global going going = True while going: img, img_path = pl.load_one_img(img_shape, dest='tmp.png') screen, px = _setup('tmp.png', 1) left, upper, right, lower, going = _mainLoop(screen, px, (mask_width, mask_height)) if not going: scipy.misc.toimage(masked_img, cmin=-1, cmax=1).save('output.png') break (x1, x2, y1, y2) = (int(left * scale), int(upper * scale), int(right * scale), int(lower * scale)) masked_img = np.empty_like(img) missing_parts = np.ndarray(shape=(x2 - x1, y2 - y1)) masked_img = img.copy() missing_parts = masked_img[y1:y2, x1:x2, :].copy() masked_img[y1:y2, x1:x2, :] = -1 guess = np.squeeze(generator.predict(np.expand_dims(masked_img, axis=0)), axis=0) masked_img[y1:y2, x1:x2, :] = guess scipy.misc.toimage(masked_img, cmin=-1, cmax=1).save('tmp.png') pygame.display.quit()
def train(self): a = auto.CCgan(self.img_cols, self.img_rows) #a.build_model() #a.train_model() a.load_model() a.load_model_weights() prepro = a.build_wrapper() import plotload test = plotload.load_one_img((256, 256), dest='green', crop=True)[0] test2 = prepro(test) train_datagen = ImageDataGenerator(rescale=1. / 255, horizontal_flip=True, vertical_flip=True, fill_mode="nearest", zoom_range=0.3, width_shift_range=0.3, height_shift_range=0.3, rotation_range=30, validation_split=0.1, preprocessing_function=prepro) val_datagen = ImageDataGenerator(rescale=1. / 255, validation_split=0.9) train_generator = train_datagen.flow_from_directory( self.train_dir, target_size=(self.img_cols, self.img_rows), batch_size=self.batch_size, class_mode="categorical") val_generator = train_datagen.flow_from_directory( self.train_dir, target_size=(self.img_cols, self.img_rows), batch_size=self.batch_size, class_mode="categorical") path, dirs, files = next(os.walk('./logs')) file_count = len(files) #checkpoint = ModelCheckpoint(f"vgg16_{file_count}.h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=True, mode='auto', period=1) #checkpoint = ModelCheckpoint(f"densenet_{file_count}.h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=True, mode='auto', period=1) checkpoint = ModelCheckpoint(f"resNet50_{file_count}.h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=True, mode='auto', period=1) early = EarlyStopping(monitor='val_acc', min_delta=0, patience=5, verbose=1, mode='auto') board = TensorBoard(f"./logs/run_{file_count}") # Train the model self.VGG.fit_generator(train_generator, epochs=10, validation_data=train_generator, callbacks=[checkpoint, early, board])
def save_img(self,epoch): test=plotload.load_one_img((256,256), dest='/home/mathias/Documents/kvasir-dataset-v2/med/stool-plenty/1.jpg', extra_dim=True) test2=A.model.predict(test) plt.subplot(121) plt.imshow(test[0]*0.5+0.5) plt.subplot(122) plt.imshow(test2[0]*0.5+0.5) plt.savefig(f"epoc_{epoch}.jpg")
def fill(self, img_adr, img_shape): import plotload as pl img, img_path = pl.load_one_img(img_shape, dest=img_adr) #WARNING; IMAGE IS NOT THE SAME AS SHOWN ON THE SCREEN, IT HAS BEEN CROPPED masked_img, _, (y1, y2, x1, x2) = self.mask_select(img, img_path) guess = np.squeeze(self.generator.predict( np.expand_dims(masked_img, axis=0)), axis=0) masked_img[y1:y2, x1:x2, :] = guess import scipy.misc scipy.misc.toimage(masked_img, cmin=-1, cmax=1).save('outfile.png')
def sort_folder(self, w, path=None): import os import cv2 from tqdm import tqdm from shutil import copyfile import sys if path is not None: dirs_i = [] dirs_o = [] d = next(os.walk(path))[1] for i in d: if i == 'none' or i == 'green' or i == 'preprocessed': continue dirs_o.append(path + 'preprocessed/' + i) dirs_i.append(path + i) for i in dirs_o: if not os.path.exists(i): os.makedirs(i) else: polyps = 'polyps' ulcerative_colitis = 'ulcerative-colitis' dirs = [polyps, ulcerative_colitis] if not os.path.exists(polyps_prep): os.makedirs(polyps_prep) if not os.path.exists(ulcerative_colitis_prep): os.makedirs(ulcerative_colitis_prep) for i, o in tqdm(zip(dirs_i, dirs_o)): for img_name in os.listdir(i): path = os.path.join(i, img_name) #img=plotload.load_one_img((self.img_cols,self.img_rows), dest=path, # extra_dim=True) if False: #cutter.is_green(img): tmp = cv2.imwrite( os.path.join(o, img_name), cv2.cvtColor(127.5 * w(img)[0] + 127.5, cv2.COLOR_RGB2BGR)) else: img = plotload.load_one_img( (self.img_cols + 70, self.img_rows + 70), dest=path, extra_dim=True) img = img[0, 10:self.img_cols + 70 - 60, 60:self.img_rows + 70 - 10] tmp = cv2.imwrite( os.path.join(o, img_name), cv2.cvtColor(127.5 * img + 127.5, cv2.COLOR_RGB2BGR))
def save_img(self, epoch): test = plotload.load_one_img( (256, 256), dest= '/home/mathias/Documents/kvasir-dataset-v2/med/stool-plenty/1.jpg', extra_dim=True) test2, _, _ = ms.mask_green_corner(test.copy(), val=-1) test2 = self.generator.predict(test2) plt.subplot(121) plt.imshow(test[0] * 0.5 + 0.5) plt.subplot(122) plt.imshow(test2[0] * 0.5 + 0.5) plt.savefig(f"epoc_{epoch}.jpg") plt.savefig(f"tmp.jpg")
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#256 # Original is ~576 self.img_rows = img_rows#256 # Original is ~720 self.channels = 3 # RGB self.img_shape=(self.img_cols,self.img_rows,self.channels) dummy=plotload.load_one_img(self.img_shape, dest='med/green',extra_dim=True) self.dims =cutter.find_square_coords(dummy) self.model=None self.pretrained=False