def create_data(folder, sameSize, maxsize): count_classes = 0 min_data = -1 data = [] label = [] print(str(len(os.listdir(folder))) + " classes found") for classe in os.listdir(folder): print(classe) if (classe == ".gitkeep"): continue print("loading classe : " + fm.make_path(folder, classe, "*.jpg") + "\n") data_current = load_pictures(fm.make_path(folder, classe, "*.jpg"), maxsize) label_current = [count_classes for i in range(len(data_current))] data.append(data_current) label.append(label_current) count_classes = count_classes + 1 if min_data == -1 or min_data > len(data_current): min_data = len(data_current) dataToReturn = [] labelToReturn = [] if sameSize: for i in range(len(data)): dataToReturn = dataToReturn + data[i][:min_data] labelToReturn = labelToReturn + label[i][:min_data] return (np.array(dataToReturn), np.array(labelToReturn))
def write_image(array, directory, file_name): fm.clean_folder(fm.make_path('polyps', 'output', 'data')) index = 0 img_store = [] for image in array: img = Image.new('RGB', config['image_size'], "white") for i in range(config['image_max']): for j in range(config['image_max']): img.putpixel((i, j), image[i][j]) n = (file_name[index].split(config['path_sep']))[-1] name = '{}'.format(n) img.save(fm.make_path(directory, name)) img_store.append(np.array(img)) index = index + 1 return img_store
def createMask(folderIn, masks): store_im = [] cpt = 0 for filename in [make_path(folderIn, name) for name in list_dir(folderIn)]: picture = pixelsVerification(load_image(filename)) for i in range(len(masks)): store_im.append(cv2.inRange(picture, masks[i], masks[i]) / 255) print("\rPicture - " + str(cpt), end='') cpt += 1 return store_im
str(round(tn, 3)) + " FN: " + str(round(fn, 3))) if __name__ == "__main__": data_augmentation.execute() batch_size = config['batch_size'] #model = model.u_net(IMG_SIZE = config['image_dimension']) #what does the Adam optimizer do model = model.u_net_batch_norm_upc() model.compile(optimizer=Adam(lr=1e-4), loss=config['loss'], metrics=config['metrics']) img_train = np.array( load_transform_pictures( fm.make_path('polyps', 'training', 'data', '*.jpg'))) img_val = np.array( load_transform_pictures( fm.make_path('polyps', 'validation', 'data', '*.jpg'))) img_test = np.array( load_transform_pictures(fm.make_path('polyps', 'test', 'data', '*.jpg'))) test_name = np.array( load_file_name(fm.make_path('polyps', 'test', 'data', '*.jpg'))) output = fm.make_path('polyps', 'output', 'classification') mask_train = np.array(data_transformation.create_binary_masks( path=fm.make_path('polyps', 'training', 'label')), dtype="float32")
# Display the first images in data classCNN.plot_data(train_dataSet, test_dataSet) # %% Model Creation model1 = classCNN.createModel(train_data.shape[1:], nClasses, config['model']) model1.compile(optimizer=config['compile']['optimizer'], loss=config['compile']['loss'], metrics=config['compile']['metrics']) model1.summary() # Open the file with open( fm.make_path(config['folderModel'], config['nameModel'], config['nameModel'] + '.txt'), 'w') as fh: # Pass the file handle in as a lambda function to make it callable model1.summary(print_fn=lambda x: fh.write(x + '\n')) # %% fit the program print("Hit a key to train the model ...") input() callBacks1 = callbacks.EarlyStopping( monitor=config['callbacks1']['monitor'], min_delta=config['callbacks1']['min_delta'], patience=config['callbacks1']['patience'], verbose=config['callbacks1']['verbose'], mode=config['callbacks1']['mode'], baseline=config['callbacks1']['baseline'],
def plot_fullInformation(predictPerCent, history, predict_succes, predict_fails, path, name, analysis): if (len(analysis) < 4): raise ValueError('plot_fullInformation : no enough analysis (<4)') nbPicturesTested = analysis[0] + analysis[1] + analysis[2] + analysis[3] cells = [["Class :", "Clean", "Dirty"], ["True", str(analysis[0]), str(analysis[2])], ["False", str(analysis[1]), str(analysis[3])]] fig = plt.figure(3) fig.clear() gs = GridSpec(2, 2) gs.update(hspace=0.4, bottom=0.05) fig.suptitle('Model name : ' + str(name), fontsize=18) gs1 = GridSpecFromSubplotSpec(1, 2, subplot_spec=gs[0, :]) ax1 = plt.subplot(gs1[0]) ax1.plot(history.history['loss'], 'r-+', linewidth=1) ax1.plot(history.history['val_loss'], 'b-+', linewidth=1) ax1.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=10, loc='upper right', borderaxespad=0.) ax1.grid(True) ax1.set_xlabel('Epochs ') ax1.set_title('Loss Curves', fontsize=12) ax2 = plt.subplot(gs1[1]) ax2.plot(history.history['acc'], 'r-+', linewidth=1) ax2.plot(history.history['val_acc'], 'b-+', linewidth=1) ax2.grid(True) ax2.set_xlabel('Epochs ') ax2.set_title('Accuracy Curves', fontsize=12) # box plot part gs2 = GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1, 0], wspace=0.3) # succes plot ax3 = plt.subplot(gs2[:, 0]) bp = ax3.boxplot(predict_succes, patch_artist=True) ax3.axis([0.75, 1.25, 0.4, 1]) ax3.set_title('Succes (' + str(round(predictPerCent, 2)) + '%)', fontsize=12) ax3.grid(True) ax3.set_ylabel('Probability given by the prediction') # fails plot ax4 = plt.subplot(gs2[:, 1]) bp2 = ax4.boxplot(predict_fails, patch_artist=True) ax4.axis([0.75, 1.25, 0.4, 1]) ax4.set_title('Fails (' + str(round(100 - predictPerCent, 2)) + '%)', fontsize=12) ax4.grid(True) for element in ['boxes']: plt.setp(bp[element], color='green') plt.setp(bp2[element], color='red') for patch in bp['boxes']: patch.set(facecolor='honeydew') for patch in bp2['boxes']: patch.set(facecolor='mistyrose') # test and table gs3 = GridSpecFromSubplotSpec(2, 4, subplot_spec=gs[1, 1]) # print the table ax5 = plt.subplot(gs3[0, 0:2]) ax5.axis('off') colorCell = [['white', 'white', 'white'], ['tab:green', 'lightgreen', 'lightgreen'], ['tab:red', 'mistyrose', 'mistyrose']] table = ax5.table(cellText=cells, loc='center', cellColours=colorCell) ax5.set_title('Analysis of the prediction', fontsize=12) ax5.text(0.5, -0.05, '(' + str(nbPicturesTested) + ' pictures)', horizontalalignment='center', fontsize=12) for i in range(3): for u in range(3): table.get_celld()[(u, i)]._loc = 'center' table.get_celld()[(u, i)].set_width = 2 table.get_celld()[(u, i)].set_height = 2 # Reading of the summary file summary = "" with open(fm.make_path(path, name, name + '.txt'), 'r') as myfile: summary += myfile.read().replace("=", "*") ax6 = plt.subplot(gs3[:, 2]) ax6.axis('off') ax6.text(0, 1, summary, fontsize=5, verticalalignment='top') ax6.set_title('Model summary', fontsize=12, horizontalalignment='left')