def visualize_channels(model, layer_name, channels, data, fname): channel_data = ut.get_output_of_layer(model, layer_name, data)[0] plt.figure() nplots = len(channels) + 1 ncols = 8 nrows = nplots // ncols if nplots % ncols: nrows += 1 # Show input image orig = data[0][0].astype(np.uint8) plt.subplot(nrows, ncols, 1) ax = plt.gca() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.imshow(orig, cmap='Greys') for idx, channel in enumerate(channels): data = channel_data[channel] img = scipy.misc.imresize(data, (RESOLUTION, RESOLUTION), interp='nearest') plt.subplot(nrows, ncols, idx + 2) ax = plt.gca() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.imshow(img, cmap='cool', alpha=1.0) plt.savefig(fname)
def visualize_channels(model, layer_name, channels, data, fname): # Run model up tp requested layer channel_data = ut.get_output_of_layer(model, layer_name, data)[0] plt.figure() nplots = len(channels) + 2 ncols = 8 nrows = nplots // ncols if nplots % ncols: nrows += 1 # Show input image orig = data[0][0].astype(np.uint8) plt.subplot(nrows, ncols, 1) ax = plt.gca() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.imshow(orig, cmap='Greys') # Show channels individually for idx, channel in enumerate(channels): data = channel_data[channel] img = scipy.misc.imresize(data, (RESOLUTION, RESOLUTION), interp='nearest') plt.subplot(nrows, ncols, idx + 2) ax = plt.gca() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) #plt.imshow(img, cmap='cool', alpha=1.0) plt.imshow(img, cmap='Greys', alpha=1.0) # Show winning channel rows = channel_data[0].shape[0] cols = channel_data[0].shape[1] mychans_flat = [ np.reshape(channel_data[c], (rows * cols, )) for c in channels ] winner = np.argmax(mychans_flat, axis=0).reshape(rows, cols) img = scipy.misc.imresize(winner, (RESOLUTION, RESOLUTION), interp='nearest') plt.subplot(nrows, ncols, nplots) ax = plt.gca() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) #plt.imshow(img, cmap='cool', alpha=1.0) plt.imshow(img, cmap='Greys', alpha=1.0) plt.savefig(fname)
def visualize_channels( model, layer_name, channels, img_, fname): img = img_.copy() img *= 2.0; img -= 1.0 # normalize to [-1,1] before feeding to model img = img.reshape( (1,) + img.shape) # Add dummy batch dimension channel_data = ut.get_output_of_layer( model, layer_name, img)[0] nplots = len( channels) + 2 # channels plus orig + overlay ncols = 1 nrows = nplots // ncols plt.figure( edgecolor='k') fig = plt.gcf() scale = 6.0 fig.set_size_inches( scale*ncols, scale*nrows) # Show input image plt.subplot( nrows, ncols, 1) ax = plt.gca() ax.get_xaxis().set_visible( False) ax.get_yaxis().set_visible( False) plt.imshow( img_) # cmap='Greys') # Show overlay plt.subplot( nrows, ncols, 2) ax = plt.gca() ax.get_xaxis().set_visible( False) ax.get_yaxis().set_visible( False) plt.imshow( img_) # orig data = channel_data[: ,:, 0] # onboardness dimg = cv2.resize( data, (img_.shape[1], img_.shape[0]), interpolation = cv2.INTER_NEAREST) plt.imshow( dimg, cmap='hot', alpha=0.5) # Show output channels for idx,channel in enumerate( channels): data = channel_data[:,:,channel] # Normalization unnecessary, done automagically #mmin = np.min(data) #data -= mmin #mmax = np.max(data) #data /= mmax dimg = cv2.resize( data, (img_.shape[1], img_.shape[0]), interpolation = cv2.INTER_NEAREST) plt.subplot( nrows, ncols, idx+3) ax = plt.gca() ax.get_xaxis().set_visible( False) ax.get_yaxis().set_visible( False) plt.imshow( dimg, cmap='hot', alpha=1.0) # # Channel 0 minus Channel 1 # chan0 = channel_data[:,:,0].astype(np.float32) # chan1 = channel_data[:,:,1].astype(np.float32) # diff = chan0 - chan1 # mmax = np.max(diff) # mmin = np.min(diff) # diff -= mmin # diff /= (mmax - mmin) # diff *= 255 # diff = diff.astype(np.uint8) # dimg = cv2.resize( diff, (img_.shape[1], img_.shape[0]), interpolation = cv2.INTER_NEAREST) # plt.subplot( nrows, ncols, nrows) # ax = plt.gca() # ax.get_xaxis().set_visible( False) # ax.get_yaxis().set_visible( False) # plt.imshow( dimg, cmap='hot', alpha=1.0) plt.tight_layout() plt.savefig( fname)
def main(): if len(sys.argv) == 1: usage(True) global GRIDSIZE, RESOLUTION parser = argparse.ArgumentParser(usage=usage()) parser.add_argument("--gridsize", required=True, type=int) parser.add_argument("--epochs", required=False, default=10, type=int) parser.add_argument("--rate", required=False, default=0, type=float) parser.add_argument("--visualize", required=False, action='store_true') args = parser.parse_args() GRIDSIZE = args.gridsize RESOLUTION = GRIDSIZE * 2 * 2 * 2 * 2 model = GCountModel(RESOLUTION, GRIDSIZE, BATCH_SIZE, args.rate) if args.visualize or not args.epochs: if os.path.exists(WEIGHTSFILE): print('Loading weights from file %s...' % WEIGHTSFILE) model.model.load_weights(WEIGHTSFILE) else: if os.path.exists(MODELFILE): print('Loading model from file %s...' % MODELFILE) model.model = km.load_model(MODELFILE, custom_objects={"th": th}) if args.rate: model.model.optimizer.lr.set_value(args.rate) print('Reading data...') images = ut.get_data(SCRIPTPATH, (RESOLUTION, RESOLUTION)) output = ut.get_output_by_key(SCRIPTPATH, 'stones') #----------------------------------------------------------- # Reshape targets to look like the flattened network output tt = output['valid_output'] valid_output = np.array([[ x.tolist().count(EMPTY), x.tolist().count(WHITE), x.tolist().count(BLACK) ] for x in tt]) tt = output['train_output'] train_output = np.array([[ x.tolist().count(EMPTY), x.tolist().count(WHITE), x.tolist().count(BLACK) ] for x in tt]) means, stds = ut.get_means_and_stds(images['train_data']) ut.normalize(images['train_data'], means, stds) ut.normalize(images['valid_data'], means, stds) # Visualization #----------------- if args.visualize: print('Dumping conv layer images to jpg') visualize_channels(model.model, 'lastconv', range(0, 3), images['train_data'][700:701], 'lastconv0.jpg') visualize_channels(model.model, 'lastconv', range(0, 3), images['train_data'][500:501], 'lastconv1.jpg') visualize_channels(model.model, 'lastconv', range(0, 3), images['train_data'][400:401], 'lastconv2.jpg') visualize_channels(model.model, 'lastconv', range(0, 3), images['train_data'][300:301], 'lastconv3.jpg') visualize_channels(model.model, 'lastconv', range(0, 3), images['train_data'][200:201], 'lastconv4.jpg') exit(0) # If no epochs, just print output and what it should have been if not args.epochs: idx = 0 print('lastconv') xx = ut.get_output_of_layer(model.model, 'lastconv', images['train_data'][idx:idx + 1]) print(xx) print('count_e') xx = ut.get_output_of_layer(model.model, 'count_e', images['train_data'][idx:idx + 1]) print(xx) print('count_w') xx = ut.get_output_of_layer(model.model, 'count_w', images['train_data'][idx:idx + 1]) print(xx) print('count_b') xx = ut.get_output_of_layer(model.model, 'count_b', images['train_data'][idx:idx + 1]) print(xx) print('out') xx = model.model.predict(images['train_data'][idx:idx + 1], batch_size=1) print(xx) print('target') print(train_output[idx:idx + 1]) BP() # Train if args.epochs: print('Start training...') model.train(images['train_data'], train_output, images['valid_data'], valid_output, BATCH_SIZE, args.epochs) model.model.save_weights(WEIGHTSFILE) model.model.save(MODELFILE)
# count1 = kl.Lambda(lambda x: K.sum(K.equal( K.ones(3*3) * 3, x)).astype('float32').reshape((1,1)), # output_shape=(1,),name='count1') (flat1) #out = kl.concatenate([count0,count1],name='out') #out = kl.concatenate([flat0,flat1],name='out') model = km.Model(inputs=inputs, outputs=sum0) #self.model.compile(loss='mse', optimizer=kopt.Adam(), metrics=[ut.element_match]) model.compile(loss='mse', optimizer=kopt.Adam(), metrics=['accuracy']) model.summary() #data = np.array([[1,2,3,4,5,6,7,8,9], [10,11,12,3,3,15,16,17,18]]).reshape((1,2,3,3)) data = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 1, 1, 1, 1, 1, 1, 1, 1]]).reshape((1, 2, 3, 3)) flat0 = ut.get_output_of_layer(model, 'flat0', data) print('flat0') print(flat0) #flat1 = ut.get_output_of_layer(model,'flat1',data) #print('flat1'); print(flat1) sum0 = ut.get_output_of_layer(model, 'sum0', data) print('sum0') print(sum0) # count0 = ut.get_output_of_layer(model,'count0',data) # print('count0'); print(count0) # count1 = ut.get_output_of_layer(model,'count1',data) # print('count1'); print(count1)
def main(): if len(sys.argv) == 1: usage(True) global GRIDSIZE, RESOLUTION RESOLUTION = GRIDSIZE * 2 * 2 * 2 parser = argparse.ArgumentParser(usage=usage()) parser.add_argument("--gridsize", required=True, type=int) parser.add_argument("--epochs", required=False, default=10, type=int) parser.add_argument("--rate", required=False, default=0, type=float) parser.add_argument("--visualize", required=False, action='store_true') args = parser.parse_args() GRIDSIZE = args.gridsize RESOLUTION = GRIDSIZE * 2 * 2 * 2 * 2 model = GoogleModel(RESOLUTION, GRIDSIZE, args.rate) if args.visualize or not args.epochs: if os.path.exists(WEIGHTSFILE): print('Loading weights from file %s...' % WEIGHTSFILE) model.model.load_weights(WEIGHTSFILE) else: if os.path.exists(MODELFILE): print('Loading model from file %s...' % MODELFILE) model.model = km.load_model(MODELFILE) if args.rate: model.model.optimizer.lr.set_value(args.rate) print('Reading data...') images = ut.get_data(SCRIPTPATH, (RESOLUTION, RESOLUTION)) output = ut.get_output_by_key(SCRIPTPATH, 'stones') #----------------------------------------------------------- # Reshape targets to look like the flattened network output tt = output['valid_output'] valid_output = np.array([ np.transpose(ut.onehot(x, NCOLORS)).reshape(GRIDSIZE * GRIDSIZE * 3) for x in tt ]) tt = output['train_output'] train_output = np.array([ np.transpose(ut.onehot(x, NCOLORS)).reshape(GRIDSIZE * GRIDSIZE * 3) for x in tt ]) means, stds = ut.get_means_and_stds(images['train_data']) ut.normalize(images['train_data'], means, stds) ut.normalize(images['valid_data'], means, stds) # Visualization #----------------- if args.visualize: print('Dumping conv layer images to jpg') visualize_channels(model.model, 'lastconv', range(0, 3), images['valid_data'][42:43], 'lastconv.jpg') exit(0) # If no epochs, just print output and what it should have been if not args.epochs: idx = 0 xx = ut.get_output_of_layer(model.model, 'out', images['train_data'][idx:idx + 1]) print(xx) print(train_output[idx:idx + 1]) BP() # Train if args.epochs: print('Start training...') model.train(images['train_data'], train_output, images['valid_data'], valid_output, BATCH_SIZE, args.epochs) model.model.save_weights(WEIGHTSFILE) model.model.save(MODELFILE)