def g_input(self,inputs): if (self.case_g=="one"): return self.g(self.z)[-1] if (self.case_g=="two"): image_size=128 z_dim=image_size*image_size z_random = np.random.uniform(-1,1,(self.batch_size*z_dim)) print(z_random.shape) z_random=np.array(z_random).reshape( 1,self.batch_size*image_size*image_size) print(z_random.shape) inputs_images=(inputs[:,:,:,0]) inputs_images=tf.reshape(inputs_images,[1,self.batch_size*image_size*image_size]) dsn=datasetnet.datasetnet('test','main') # need the test data from ROI to create synthetic images for endo and epi seperatly dsn.image_shape= 128 x, z_random=dsn.Gan_preprocessing_images(inputs_images,z_random) input_masks = tf.concat([inputs_images, z_random], 0) return self.g(input_masks)[-1]
def pass_train_roi_data(self, phase, code): ''' pass the X and Y dimmensions ''' #test in train datasets prediction_image, prediction_mask_total = [], [] sm = store_model.store_model() dsn = datasetnet.datasetnet("train_pre", "roi") Xtest, X_total, Y, contour_mask, storetxt = dsn.create_dataset() dsn_roi_mask = datasetnet.datasetnet( phase, code ) #The size that the data will be store depend in "test", "train" "roi" "main" if Xtest == []: print(" no training ... ") print(" Continue ... ") else: pred, metric = sm.load_best_callback(Xtest, Y, 'roi', self.roi_model_name) print('Final train accuracy:', (metric[1] * 100.0)) print(pred.shape) # store images length = len(Xtest) classes = self.classes o = 0 while o < classes: prediction_mask = [] #output, mask_roi, label_mask = dsn_roi_mask.compute_pred_roi(X_total[0], pred[0],contour_mask[0,:,:,0]) #print(np.array(label_mask)) for i in range(0, int(length)): output, mask_roi, label_mask = dsn_roi_mask.compute_pred_roi( X_total[i], pred[i], contour_mask[i, :, :, o]) #save to images the results if (self.save_mode == 'on'): str2 = self.STORE_PATH1 + 'ROImasks%s.%s' % (i + ( (o) * (length)), self.counter_extention) #cv2.imwrite(str2, label_mask) pylab.imsave(str2, label_mask, format=self.data_extention, cmap=cm.Greys_r) if o == 0: str1 = self.STORE_PATH1 + 'ROItrain%s.%s' % ( i, self.data_extention) cv2.imwrite(str1, output) #pylab.imsave(str1, output, format=self.data_extention, cmap = cm.Greys_r) prediction_mask.append(label_mask) #print(np.array(prediction_mask).shape) if o == 0: prediction = np.array(output) prediction_image.append(prediction) prediction_mask = np.array(prediction_mask) if o == 0: prediction_mask = np.reshape( (prediction_mask), [(prediction_mask).shape[0], 1, (prediction_mask).shape[1], (prediction_mask).shape[2]]) prediction_mask_total = prediction_mask prediction_image = np.reshape(np.array(prediction_image), [ np.array(prediction_image).shape[0], np.array(prediction_image).shape[1], np.array(prediction_image).shape[2], 1 ]) else: prediction_mask = np.reshape( (prediction_mask), [(prediction_mask).shape[0], 1, (prediction_mask).shape[1], (prediction_mask).shape[2]]) prediction_mask_total = np.append(prediction_mask_total, prediction_mask, axis=1) prediction_mask_total = np.reshape( np.array(prediction_mask_total), [ np.array(prediction_mask_total).shape[0], np.array(prediction_mask_total).shape[1], np.array(prediction_mask_total).shape[2], np.array(prediction_mask_total).shape[3] ]) o = o + 1 if (self.data_return == 'on'): return prediction_image, prediction_mask_total
def pass_test_main_data(self): prediction_image, prediction_mask_total = [], [] sm = store_model.store_model() dsn_main_test = datasetnet.datasetnet('test', 'main') if (self.restore_from_jpg_tif == 'on'): mainXtest2, mainX_total2, mainY2, maincontour_mask2, mainstoretxt2 = dsn_main_test.create_dataset( ) mainXtest = mainX_total2 else: data_return_store = self.data_return save_mode_store = self.save_mode self.data_return = 'on' self.save_mode = 'on' mainXtest, mainY2 = self.pass_test_roi_data('test', 'main') self.data_return = data_return_store self.save_mode = save_mode_store mainXtest2 = mainXtest if mainXtest2 == []: print(" no testing ... ") print(" Continue ... ") else: mainpred2, mainmetric2 = sm.load_best_callback( mainXtest2, mainY2, 'main', self.main_model_name) # store images vo = vision_outputs('main') vo.store_test_results(mainmetric2) print('Final test loss:', (mainmetric2[0])) print('Final test accuracy:', (mainmetric2[1] * 100.0)) print('Final test accuracy:', (mainmetric2[2] * 100.0)) print('Final test accuracy:', (mainmetric2[3] * 100.0)) print(mainpred2[1, 10, 50, 0], mainpred2[10, 100, 50, 0], mainpred2[19, 10, 50, 1], mainpred2[50, 20, 100, 1]) # store images length2 = len(mainXtest2) classes2 = self.classes o = 1 while o < classes2 + 2: prediction_mask, prediction_mask_old = [], [] for i in range(0, length2): #print(pred[0].shape) #print(Xtest[i].shape) mainoutput2 = mainXtest[i] mainlabel_mask2 = 255 * mainpred2[i, :, :, o - 1] if (self.save_mode == 'on'): str2 = self.STORE_PATH_main2 + 'mainmasks%s_rgb' % ( i + ((o - 1) * (length2))) str3 = self.STORE_PATH_main2 + 'mainmask_%s.%s' % ( i + ((o - 1) * (length2)), self.counter_extention) #cv2.imwrite(str3, mainlabel_mask2) pylab.imsave(str3, mainlabel_mask2, format=self.data_extention, cmap=cm.Greys_r) if o == 1: str1 = self.STORE_PATH_main2 + 'maintest%s.%s' % ( i, self.data_extention) str4 = self.STORE_PATH_main2 + 'maintest%s_rgb' % ( i) cv2.imwrite(str1, mainoutput2) if (self.data_return == 'on'): prediction_mask.append(mainlabel_mask2) print(np.array(prediction_mask).shape) if o == 1: prediction = np.array(mainoutput2) prediction_image.append(prediction) if (self.data_return == 'on'): prediction_mask = np.array(prediction_mask) if o == 1: prediction_mask = np.reshape( (prediction_mask), [(prediction_mask).shape[0], 1, (prediction_mask).shape[1], (prediction_mask).shape[2]]) prediction_mask_total = (prediction_mask) prediction_image = np.reshape( np.array(prediction_image), [ np.array(prediction_image).shape[0], np.array(prediction_image).shape[1], np.array(prediction_image).shape[2], 1 ]) else: print( np.array(prediction_mask_total).shape, np.array(prediction_mask).shape) prediction_mask = np.reshape( (prediction_mask), [(prediction_mask).shape[0], 1, (prediction_mask).shape[1], (prediction_mask).shape[2]]) prediction_mask_total = np.append( prediction_mask_total, prediction_mask, axis=1) prediction_mask_total = np.reshape( np.array(prediction_mask_total), [ np.array(prediction_mask_total).shape[0], np.array(prediction_mask_total).shape[1], np.array(prediction_mask_total).shape[2], np.array(prediction_mask_total).shape[3] ]) o = o + 1 if (self.data_return == 'on'): return prediction_image, prediction_mask_total
def pass_train_main_data(self): #prediction_image,prediction_mask_total=[],[] sm = store_model.store_model() dsn_main_train = datasetnet.datasetnet('train', 'main') if (self.restore_from_jpg_tif == 'on'): mainXtest2, mainX_total, mainY, maincontour_mask, mainstoretxt = dsn_main_train.create_dataset( ) mainXtest = mainX_total else: data_return_store = self.data_return save_mode_store = self.save_mode self.data_return = 'on' #self.save_mode='off' mainXtest, mainY = self.pass_train_roi_data('train', 'main') self.data_return = data_return_store self.save_mode = save_mode_store mainXtest2 = mainXtest if mainXtest == []: print(" no testing ... ") print(" Continue ... ") else: mainpred, mainmetric = sm.load_best_callback( mainXtest, mainY, 'main', self.main_model_name) print('Final train loss:', (mainmetric[0])) print('Final train accuracy:', (mainmetric[1] * 100.0)) print('Final train accuracy:', (mainmetric[2] * 100.0)) print('Final train accuracy:', (mainmetric[3] * 100.0)) print(mainXtest.shape) print(mainpred.shape) # store images # store images length = len(mainXtest) classes = self.classes o = 1 #mainpred=np.array(mainpred,dtype='uint8') while o < classes + 1: prediction_mask = [] #print(np.array(mainXtest[0,:,:,0])) #print(np.array(mainXtest[0,:,:,0],dtype='uint8')) #print(np.array(mainY[0,:,:,0])) #print(np.array(mainY[0,:,:,0],dtype='uint8')) print(np.array(mainpred[0, :, :, 0])) print(np.array(mainpred[0, :, :, 1])) for i in range(0, length): #print(Xtest[i]) mainlabel_mask = mainpred[i, :, :, o - 1] mainoutput = dsn_main_train.compute_pred_main( mainXtest[i], mainpred[i]) if (self.save_mode == 'on'): str2 = self.STORE_PATH_main1 + 'mainmasks%s.%s' % ( i + ((o - 1) * (length)), self.counter_extention) #cv2.imwrite(str2, mainlabel_mask) pylab.imsave(str2, mainlabel_mask, format=self.data_extention, cmap=cm.Greys_r) if o == 1: str1 = self.STORE_PATH_main1 + 'maintrain%s.%s' % ( i, self.data_extention) pylab.imsave(str1, mainoutput, format=self.data_extention, cmap=cm.Greys_r) #mainoutput #cv2.imwrite(str1,mainoutput) o = o + 1
def pass_test_roi_data(self, phase, code): ''' pass the X and Y dimmensions ''' #test in test dataset sm = store_model.store_model() prediction_image, prediction_mask_total = [], [] dsn2 = datasetnet.datasetnet("test", "roi") Xtest2, X_total2, Y2, contour_mask2, storetxt2 = dsn2.create_dataset() dsn_roi_mask_test = datasetnet.datasetnet(phase, code) if Xtest2 == []: print(" no testing ... ") print(" Continue ... ") else: pred2, metric2 = sm.load_best_callback(Xtest2, Y2, 'roi', self.roi_model_name) print('Final test accuracy:', (metric2[1] * 100.0)) print(pred2.shape) length2 = len(Xtest2) classes2 = self.classes o = 0 while o < classes2: prediction_mask = [] for i in range(0, length2): #print(pred[0].shape) #print(Xtest[i].shape) output2, mask_roi2, label_mask2 = dsn_roi_mask_test.compute_pred_roi( X_total2[i], pred2[i], contour_mask2[i, :, :, o]) if (self.save_mode == 'on'): str2 = self.STORE_PATH2 + 'ROImasks%s.%s' % (i + ( (o) * (length2)), self.counter_extention) #cv2.imwrite(str2, label_mask2) pylab.imsave(str2, label_mask2, format=self.data_extention, cmap=cm.Greys_r) if o == 0: str1 = self.STORE_PATH2 + 'ROItest%s.%s' % ( i, self.data_extention) #pylab.imsave(str1, output2, format=self.data_extention, cmap = cm.Greys_r) cv2.imwrite(str1, output2) #print(str2) prediction_mask.append(label_mask2) #print(np.array(prediction_mask).shape) if o == 0: prediction = np.array(output2) prediction_image.append(prediction) prediction_mask = np.array(prediction_mask) if o == 0: prediction_mask = np.reshape( (prediction_mask), [(prediction_mask).shape[0], 1, (prediction_mask).shape[1], (prediction_mask).shape[2]]) prediction_mask_total = (prediction_mask) prediction_image = np.reshape(np.array(prediction_image), [ np.array(prediction_image).shape[0], np.array(prediction_image).shape[1], np.array(prediction_image).shape[2], 1 ]) else: prediction_mask = np.reshape( (prediction_mask), [(prediction_mask).shape[0], 1, (prediction_mask).shape[1], (prediction_mask).shape[2]]) prediction_mask_total = np.append(prediction_mask_total, prediction_mask, axis=1) prediction_mask_total = np.reshape( np.array(prediction_mask_total), [ np.array(prediction_mask_total).shape[0], np.array(prediction_mask_total).shape[1], np.array(prediction_mask_total).shape[2], np.array(prediction_mask_total).shape[3] ]) o = o + 1 print( np.array(prediction_mask_total).shape, np.array(prediction_image).shape) if (self.data_return == 'on'): return prediction_image, prediction_mask_total
#Author: Michail Mamalakis #Version: 0.1/test #Licence: #email:[email protected] from auto_segm.regularization import data_augmentation from auto_segm import datasetnet, config import numpy as np import cv2 args = config.parse_arguments() STORE_PATH=args.store_data_test roi_shape=args.roi_shape_roi original_image_shape=args.original_image_shape_roi image_shape=args.image_shape_roi dsnr=datasetnet.datasetnet('train','roi') X, X_fullsize, Y, contour_mask, storetxt, images = dsnr.create_dataset() # looop in the iterator to create all the augmented data Xaug, Yaug= [], [] for i in data_augmentation(X,Y): Xaug.append(i[0]) Yaug.append(i[1]) # add the initial data Xaug.append(X) Yaug.append(Y) #reshape to (total_image,height,weight,channels) Xaug1, Yaug2= np.asarray(Xaug) , np.asarray(Yaug) Xaug1=Xaug1.reshape((Xaug1.shape[0]*Xaug1.shape[1],Xaug1.shape[2],Xaug1.shape[3],Xaug1.shape[4])) Yaug2= Yaug2.reshape((Yaug2.shape[0]*Yaug2.shape[1],Yaug2.shape[2],Yaug2.shape[3],Yaug2.shape[4])) print(Yaug2.shape, Xaug1.shape)
#Author: Michail Mamalakis #Version: 0.1 #Licence: #email:[email protected] from __future__ import division, print_function from auto_segm import datasetnet, handle_data dsn=datasetnet.datasetnet("train_pre","roi") X, X_total, Y, contour_mask, storetxt = dsn.create_dataset() h_d=handle_data.handle_data(X,Y) training_augment, train_steps_per_epoch, validation_augment, val_steps_per_epoch =h_d.validation_data() print("validation data handle test passed") training_augment2, train_steps_per_epoch2 =h_d.no_validation_data() print("no validation data handle test passed")
def gan_synthetic_data_generate(self): ''' Run the Generative Adversarial Network train model synthesis images ''' print("Generate synthetic data") #if case=='main': image_size = 128 #self.original_image_shape_main, instead of 284(modify this so to take the image shape TODO ) #else: # image_size=self.original_image_shape_roi checkpoint_folder = self.gan_train_directory + "/" + self.gancheckpoint + "/" #+ '/checkpoint%d.ckpt' % self.stored_cycle_number #tf.reset_default_graph() dcgan = DCGAN( batch_size=self.batch_size) # define both classes of masks o = 0 dsn = datasetnet.datasetnet( 'test', 'main' ) # need the test data from ROI to create synthetic images for endo and epi seperatly dsn.image_shape = 128 r_masks, resize_masks = [], [] while o < 1: #self.classes: r_mask_store = resize_masks resize_masks, images_resize = [], [] for i in range(self.batch_size): image_resize = cv2.resize( self.X[i, :, :, 0].reshape(self.X.shape[1], self.X.shape[2]), (image_size, image_size), cv2.INTER_NEAREST) resize_mask = cv2.resize( self.Y[i, o, :, :].reshape(self.Y.shape[2], self.Y.shape[3]), (image_size, image_size), cv2.INTER_NEAREST) resize_mask = np.array(resize_mask).reshape( 1, image_size, image_size) resize_masks.append(resize_mask) images_resize.append(image_resize) if o > 0: r_masks = np.append(r_mask_store, resize_masks, axis=1) else: r_masks = resize_masks o = o + 1 mask_resize = np.array(r_masks).reshape(self.batch_size, image_size, image_size, 1) #Y.shape[1])) images_resize = np.array(images_resize).reshape( self.batch_size, image_size, image_size, 1) #Y.shape[1])) X_pre, Y_pre = dsn.Gan_preprocessing_images(images_resize, mask_resize) #print(X_pre.shape, Y_pre.shape) image_structure = tf.concat([X_pre, Y_pre], 3) imgs = dcgan.test_images( row=8, col=8, num_samples=None, inputs=None, checkpoint_folder=checkpoint_folder, num_synthetic_images=self.num_synthetic_images, image_structure=image_structure) for k in range(self.num_synthetic_images): imgs_folder = os.path.join(self.gan_train_directory, 'synthetic_images/images/') if not os.path.exists(imgs_folder): os.makedirs(imgs_folder) segs_folder = os.path.join(self.gan_train_directory, 'synthetic_images/masks/') if not os.path.exists(segs_folder): os.makedirs(segs_folder) img_channel = imgs[k][:, :, 0] img_seg = imgs[k][:, :, 1] imsave(os.path.join(imgs_folder, 'img_%d.jpeg') % k, img_channel.reshape(image_size, image_size), cmap=cm.Greys_r) imsave(os.path.join(segs_folder, 'segmented_%d.jpeg') % k, img_seg.reshape(image_size, image_size), cmap=cm.Greys_r)