def main(): #load/create model dataFolderPath = os.getcwd() + '/data_test_orient' modelFolder = dataFolderPath + '/Model' if hasattr(args, 'load_path') and args.load_path is not None: print("Loading Model from: " + args.load_path) fileName = args.load_path model = e.load_model(fileName) print("Model Loaded") else: print("Creating new model") model = e.create_model_seg() #set training parameters sgd = opt.SGD(lr=0.0001, decay=0.0005, momentum=0.9, nesterov=True) model.compile(loss='mean_squared_error', optimizer='sgd') topResults, bottomResults, im, label = e.evaluate_top_and_bottom_k( model=model, dataFolderPath=dataFolderPath, k=1) #Display image, label and predicted output for the image with highest error top1Fig = plt.figure(1, figsize=(15, 8)) plt.title('Input Image', loc='left') plt.title('Actual Label', loc='center') plt.title('Predicted Label', loc='right') plt.axis('off') disp_images(top1Fig, topResults[0, :, :, :], 1, 3, pad=1, cmap=cm.binary) #save the results figure resultsFolderName = modelFolder + '/results' if not os.path.exists(resultsFolderName): create_results_folder(resultsFolderName) resultFigFileName = resultsFolderName + '/' + 'top1' + '.png' plt.savefig(resultFigFileName) #Display image, label and predicted output for the image with lowest error bottom1Fig = plt.figure(2, figsize=(15, 8)) plt.title('Input Image', loc='left') plt.title('Actual Label', loc='center') plt.title('Predicted Label', loc='right') plt.axis('off') disp_images(bottom1Fig, bottomResults[0, :, :, :], 1, 3, pad=1, cmap=cm.binary) #save the results figure resultFigFileName = resultsFolderName + '/' + 'bottom1' + '.png' plt.savefig(resultFigFileName) plt.show()
def test_conv(fig, bias): if bias: weights = np.zeros((3, 3, 1, 5)) weights[:, :, 0, 0] = getSobelMask() weights[:, :, 0, 1] = getPrewitMask() weights = [weights, np.ones(5)] else: weights = np.zeros(1, 3, 3, 1, 5) weights[0, :, :, 0, 0] = getSobelMask() weights[0, :, :, 0, 1] = getPrewitMask() #weights = getSobelMask(); #print weights.shape; # create model with single layers 5 filters each of size 3 X 3 model = Sequential() model.add(ZeroPadding2D((1, 1), input_shape=(20, 20, 1))) layer = Convolution2D(5, 3, 3, dim_ordering='tf', weights=weights, bias=bias) model.add(layer) #create image with a horizontal line im = np.zeros(400) im.shape = (1, 20, 20, 1) im[0, 10, :, 0] = 1 predicted = model.predict(im) #Display image, and output for each of the five filters print 'test_conv: Image Shape' + str(im.shape) print 'test_conv: Predi Shape' + str(predicted.shape) im1 = im[0, :, :, 0] pr1 = predicted[0, :, :, 0] pr2 = predicted[0, :, :, 1] pr3 = predicted[0, :, :, 2] pr4 = predicted[0, :, :, 3] pr5 = predicted[0, :, :, 4] im1.shape = (20, 20, 1) pr1.shape = (20, 20, 1) pr2.shape = (20, 20, 1) pr3.shape = (20, 20, 1) pr4.shape = (20, 20, 1) pr5.shape = (20, 20, 1) imagesToDisplay = np.concatenate((im1, pr1, pr2, pr3, pr4), axis=2) disp_images(fig, imagesToDisplay, 2, 3, pad=1, cmap=cm.binary) return model
from lib_image_display import disp_images; import numpy as np; import matplotlib.pyplot as plt; ##def disp_images(fig): ## grid = ImageGrid(fig, 111, nrows_ncols=(2, 3),axes_pad=0.1,label_mode="1",share_all=True,cbar_location="right",cbar_mode="each",cbar_size="7%",cbar_pad="2%"); ## for i in range(6) : ## imFromImShow = grid[i].imshow(im); ## grid.cbar_axes[i].colorbar( imFromImShow ) im = np.arange(1000); im.shape = 10, 10,10; fig = plt.figure(1, (15, 15)); disp_images(fig,im,2,3); plt.show();
def main(): dataFolder = os.getcwd() + '/data_prev' modelFolder = dataFolder + '/latestModel' #load first model if hasattr(args, 'model1_path') and args.model1_path is not None: print("Loading Model from: " + args.model1_path) model1Folder = args.model1_path fileName = modelFolder + '/Keras_model_weights.h5' model1 = e.load_model(fileName) print("Model Loaded") else: print("Creating new model") model = e.create_model_seg() #set training parameters sgd = opt.SGD(lr=0.0001, decay=0.0005, momentum=0.9, nesterov=True) model1.compile(loss='mean_squared_error', optimizer='sgd') #load second model if hasattr(args, 'model2_path') and args.model2_path is not None: print("Loading Model from: " + args.model2_path) model2Folder = args.model2_path fileName = model2Folder + '/Keras_model_weights.h5' model2 = e.load_model(fileName) print("Model Loaded") else: print("Creating new model") model2 = e.create_model_seg() #set training parameters sgd = opt.SGD(lr=0.0001, decay=0.0005, momentum=0.9, nesterov=True) model2.compile(loss='mean_squared_error', optimizer='sgd') topResults, bottomResults, im, label = e.evaluate_top_and_bottom_k( model1, dataFolder) topResults2, bottomResults2, im2, label2 = e.evaluate_top_and_bottom_k( model2, dataFolder) #Display image, label and predicted output for the image with highest error imagesToshowTop = np.zeros((400, 200, 3)) imagesToshowTop[:, :, 0] = topResults[:, :, 1] imagesToshowTop[:, :, 1] = topResults[:, :, 2] imagesToshowTop[:, :, 2] = topResults2[:, :, 2] top1Fig = plt.figure(1, figsize=(15, 8)) plt.title('Label', loc='left') plt.title('Model-1 Result', loc='center') plt.title('Model-2 Result', loc='right') plt.axis('off') disp_images(top1Fig, imagesToshowTop, 1, 3, pad=1, cmap=cm.binary) #save the results figure resultsFolderName = dataFolder + '/results' if not os.path.exists(resultsFolderName): create_results_folder(resultsFolderName) resultFigFileName = resultsFolderName + '/' + 'top1' + '.png' plt.savefig(resultFigFileName) #Display image, label and predicted output for the image with lowest error imagesToshowBottom = np.zeros((400, 200, 3)) imagesToshowBottom[:, :, 0] = bottomResults[:, :, 1] imagesToshowBottom[:, :, 1] = bottomResults[:, :, 2] imagesToshowBottom[:, :, 2] = bottomResults2[:, :, 2] bottom1Fig = plt.figure(2, figsize=(15, 8)) plt.title('Label', loc='left') plt.title('Model-1 Result', loc='center') plt.title('Model-2 Result', loc='right') plt.axis('off') disp_images(bottom1Fig, imagesToshowBottom, 1, 3, pad=1, cmap=cm.binary) #save the results figure resultFigFileName = resultsFolderName + '/' + 'bottom1' + '.png' plt.savefig(resultFigFileName) plt.show()