def __init__(self,dataset,target): self.dataset=dataset self.target=target self.attribute="" self.value=0 self.avg = util.getAvg(dataset, target) self.numberOfRows=dataset.shape[0] self.childList = {} # return if the target attribute has only a unique value if self.dataset[self.target].unique().shape[0] == 1: self.loss = util.loss(np.array(self.dataset[self.target]), np.repeat([self.avg], self.dataset[self.target].shape[0])) return self.standardDeviation=util.standardDeviation(dataset,target) self.cof=self.standardDeviation/self.avg #creating the model and computing the loss of the node self.model=util.getLinearClassifier() # check if train test split would actually result in usable data sets (if not set loss to minimal and return) if 0.3*dataset.shape[0] < 1: self.loss = util.loss(np.array(self.dataset[self.target]), np.repeat([self.avg], self.dataset[self.target].shape[0])) return #print("Endnode average: ", self.avg) #print("Endnode data point values: ", self.dataset[target]) else: X_train, X_test, y_train, y_test = train_test_split(dataset, dataset[target], test_size=0.3,train_size=0.7,shuffle=True) util.fitLinearRegressor(X_train, y_train, self.model) self.prediction = util.predict(self.model, X_test) self.loss = util.loss(y_test, self.prediction) self.split()
def main(): arguments = parser.parse_args() if not os.path.exists(arguments.input): print('Error: Given input file does not exist') return if not os.path.exists(arguments.checkpoint): print('Error: Given checkpoint file does not exist') return if not os.path.exists(arguments.category_names): print('Error: Given category to name mapping file does not exist') return if arguments.use_gpu: if torch.cuda.is_available(): device = 'cuda' else: print('Error: System does not support CUDA') return model = torch.load(arguments.checkpoint) top_p, top_class = utilities.predict(arguments.input, model, device, topk=arguments.top_k) with open(arguments.category_names, 'r') as f: cat_to_name = json.load(f) top_label = list(map(cat_to_name.get, top_class)) predictions = dict(zip(top_label, top_p)) print('Neural network predicted the following categories with corresponding probabilities:\n\r', predictions)
def main(): model, class_to_idx = utilities.load_checkpoint(file_path) map_location = 'cpu' strict = False img_datasets, data_loaders = utilities.transform_load_data(data_directory) with open(category_names, 'r') as f: cat_to_name = json.load(f) probs, classes = utilities.predict(path_image, model, number_of_outputs, device) print("THERE ARE THE PROBABILITIES: ", probs) print("THESE ARE THE CLASSES: ", classes) class_names = img_datasets['train'].classes flower_names = [cat_to_name[class_names[e]] for e in classes] print("THESE ARE THE TOP 5 CATEGORY FLOWER NAMES: ", flower_names)
def main(): #Load checkpoint from saved path if available model=utilities.load_checkpoint(path) #get categories from json file with open('cat_to_name.json', 'r') as json_file: cat_to_name = json.load(json_file) #Predict picture category using trained model probabilities = utilities.predict(path_image, model, number_of_outputs) #Get labels and probabilities of prediciton for printing labels = [cat_to_name[str(index + 1)] for index in np.array(probabilities[1][0])] probability = np.array(probabilities[0][0]) i=0 while i < number_of_outputs: print("{} with a probability of {}".format(labels[i], probability[i])) i += 1
def save_depth(): # Custom object needed for inference and training custom_objects = { 'BilinearUpSampling2D': BilinearUpSampling2D, 'depth_loss_function': None } print('Loading model...') # Load model into GPU / CPU model = load_model(args.model, custom_objects=custom_objects, compile=False) print('\nModel loaded ({0}).'.format(args.model)) # Input images inputs = load_images(glob.glob(args.input)) print('\nLoaded ({0}) images of size {1}.'.format(inputs.shape[0], inputs.shape[1:])) # Compute results outputs = predict(model, inputs) save_images('result.png', outputs, is_colormap=False)
def predict_slots(): varContent = t1.get("1.0", "end-1c") print("Predicting...") original_tokens, return_tags = predict(varContent, model) zipped_tags = list(zip( original_tokens, *return_tags)) # a list of tokens, where token [0] is original text print(zipped_tags) listbox_word.delete(0, END) listbox_token.delete(0, END) for r, tokens in enumerate(zipped_tags): if (len(tokens) >= 2): word = tokens[0] tag = " / ".join(tokens[1:]) listbox_word.insert(END, word) listbox_token.insert(END, tag) else: word = tokens[0] listbox_word.insert(END, word) listbox_token.insert(END, "No relevant slots!") print('Printed on GUI.')
# 4 Calculate Loss: softmax --> cross entropy loss loss = loss + criterion(outputs[idx_loader], labels) # 5. Getting gradients w.r.t. parameters loss.backward() # 6. Updating parameters optimizer.step() myModel.eval() dev_pred_labels = [[] for i in range(n_tasks)] test_pred_labels = [[] for i in range(n_tasks)] dev_pred = utilities.predict(myModel, dev_loader, seq_size, SETTINGS["EMBEDDING_DIM"], DEVICE, n_tasks) lb = [] for k in range(n_tasks): lb.append(utilities.LabelRepresentation()) if k == 0: if SETTINGS["TAGGING_SCHEME_DEV"] == "BIO" or SETTINGS[ "TAGGING_SCHEME_DEV"] == "IO": lb[k].use_ner_noprefix_labels() if SETTINGS["TAGGING_SCHEME_DEV"] == "BINARY": lb[k].use_ner_binary_labels() else: if SETTINGS["TAGGING_SCHEME_DEV"] == "BIO" or SETTINGS[ "TAGGING_SCHEME_DEV"] == "IO": lb[k].use_synthetic_noprefix_labels()
fourcc = cv2.VideoWriter_fourcc(*'mp4v') output_capture = cv2.VideoWriter(output_path, fourcc, 30, (448, 448)) while capture.isOpened(): ret, frame = capture.read() #NB: `frame` is a numpy array frame_counter += 1 if ret: percent = (frame_counter / total_frames) * 100 print(f"- [{percent:.0f}%] Processing frame {frame_counter}") image_PIL = Image.fromarray(frame) old_size = image_PIL.size image_PIL = image_PIL.resize(_IMAGE_SIZE_, Image.ANTIALIAS) image = transform(image_PIL).unsqueeze(0) predictions = predict(model, image, 0.6)[0] if len(predictions) > 0: for idx in range(0, predictions.size(0)): bbox = predictions[idx, :][0] try: pred_class = class_names[int(bbox[5])] print( f"\t-> Predicted {pred_class} with bounding box: {bbox}" ) draw_detection(image_PIL, bbox[:4] / _IMAGE_SIZE_[0], pred_class) except: print(bbox) image_PIL = image_PIL.resize(old_size, Image.ANTIALIAS)
ap.add_argument('--top_k', dest="top_k", action="store", default=1, type=int) ap.add_argument('--category_names', dest="category_names", action="store", default='./cat_to_name.json') ap.add_argument('--gpu', dest="gpu", action="store_true") arguments = ap.parse_args() input_image = arguments.base_input[0] checkpoint = arguments.base_input[1] top_k = arguments.top_k category_names = arguments.category_names use_gpu = arguments.gpu and torch.cuda.is_available() model, optimizer, criterion = utilities.load_checkpoint(checkpoint) results = utilities.predict(input_image, model, use_gpu=use_gpu, top_k=top_k) if use_gpu: probabilities = results[0].cpu().numpy()[0] else: probabilities = results[0].numpy()[0] if use_gpu: names = results[1].cpu().numpy()[0] else: names = results[1].numpy()[0] print("----- Results: -------") for i in range(top_k): print("{} with a probability of {:.2%}".format( utilities.get_category_name(category_names, names[i]),
rhf = RandomVerticalFlip(probability=1.0) if args.image_path: print(f"-> Detecting objects in '{args.image_path}'") with torch.no_grad(): image = Image.open(args.image_path).convert('RGB') old_size = image.size image = image.resize(_IMAGE_SIZE_, Image.ANTIALIAS) image_ = transform(image).unsqueeze(0) # image.show() flipped_image, _ = rhf((image, [])) # flipped_image.show() batch_idx = 0 start_time = time.time() predictions = predict(model, image_, 0.6) #[B,N,1,6] or [B,1,1,6] elapsed = time.time() - start_time predictions = predictions[batch_idx] #[N,1,6] # for bbox in predictions: for idx in range(0, predictions.size(0)): bbox = predictions[idx, :][0] print(bbox) pred_class = int(bbox[5]) draw_detection(image, bbox[:4] / _IMAGE_SIZE_[0], class_names[pred_class]) image.resize(old_size, Image.ANTIALIAS) image.show() print(f"Total time taken {elapsed//60:.0f}m {elapsed%60:.0f}s")
ap.add_argument('--category_names', dest="category_names", action="store", default='cat_to_name.json') ap.add_argument('--gpu', default="gpu", action="store", dest="gpu") pa = ap.parse_args() path_image = pa.input_img number_of_outputs = pa.top_k device = pa.gpu input_img = pa.input_img path = pa.checkpoint utilities.load_checkpoint(path) with open('cat_to_name.json', 'r') as json_file: cat_to_name = json.load(json_file) probabilities = utilities.predict(path_image, model, number_of_outputs, device) labels = [ cat_to_name[str(index + 1)] for index in np.array(probabilities[1][0]) ] probability = np.array(probabilities[0][0]) i = 0 while i < number_of_outputs: print("{} with a probability of {}".format(labels[i], probability[i])) i += 1 print("***************Prediction Complete****************")
def gradient_trainer(config, loss, model, full_batch, val_batch, test_network): step = 0 lr = config.lr learning_rate = lambda: lr reg = lambda: tf.reduce_sum([tf.reduce_sum(tf.square(p)) for p in model.trainable_weights]) reg_const = 1/(2*config.C) loss_with_reg = lambda y_true, y_pred: reg_const*reg() + tf.reduce_mean(tf.reduce_sum(loss(y_true, y_pred), axis=1)) if config.optim == 'SGD': optimizer = tf.keras.optimizers.SGD( learning_rate=learning_rate, momentum=config.momentum) elif config.optim == 'Adam': optimizer = tf.keras.optimizers.AdamOptimizer(learning_rate=learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08) train_inputs, train_labels = full_batch num_data = train_labels.shape[0] num_iters = math.ceil(num_data/config.bsize) print(config.args) if not config.screen_log_only: log_file = open(config.log_file, 'w') print(config.args, file=log_file) print('-------------- initializing network by methods in He et al. (2015) --------------') init_model(model.trainable_weights) total_running_time = 0.0 best_acc = 0.0 for epoch in range(0, args.epoch): loss_avg = 0.0 start = time.time() for i in range(num_iters): load_time = time.time() idx = np.random.choice(np.arange(0, num_data), size=config.bsize, replace=False) batch_input = train_inputs[idx] batch_labels = train_labels[idx] batch_input = np.ascontiguousarray(batch_input) batch_labels = np.ascontiguousarray(batch_labels) config.elapsed_time += time.time() - load_time batch_loss = minimize(batch_input, batch_labels, model, optimizer, loss_with_reg) # print initial loss if epoch == 0 and i == 0: tf.print('initial f (reg + avg. loss of 1st batch):', batch_loss) # TODO: output to log_file if not config.screen_log_only loss_avg = loss_avg + batch_loss # print log every 10% of the iterations if i % math.ceil(num_iters/10) == 0: end = time.time() tf.print('Epoch ', epoch, ': ', i, '/', num_iters, ' | loss ', batch_loss, ' | lr ', lr, ' | elapsed time ', end-start, sep='') # TODO: output to log_file if not config.screen_log_only # adjust learning rate for SGD by inverse time decay if args.optim != 'Adam': lr = config.lr/(1 + args.lr_decay*step) step = step + 1 # exclude data loading time for fair comparison epoch_end = time.time() - config.elapsed_time total_running_time += epoch_end - start config.elapsed_time = 0.0 if val_batch is None: tf.print('In epoch', epoch, 'train loss:', loss_avg/(i+1), '| epoch time', epoch_end-start) # TODO: output to log_file if not config.screen_log_only else: if test_network == None: val_loss, val_acc, _ = predict( loss, model, test_batch=val_batch, bsize=config.bsize ) else: # A separat test network part have been done... val_loss, val_acc, _ = predict( loss, model, test_batch=val_batch, bsize=config.bsize ) tf.print('In epoch ', epoch, ' train loss: ', loss_avg/(i+1), ' | val loss: ', val_loss, ' | val accuracy: ', val_acc*100, ' % | epoch time ', epoch_end-start, sep='') # TODO: output to log_file if not config.screen_log_only if val_acc > best_acc: best_acc = val_acc model.save_weights(config.model_file) print('Saved best model in {}'.format(config.model_file)) if val_batch is None: model.save_weights(config.model_file) print('Model at the last iteration saved in {}\r\n'.format(config.model_file)) output_str = 'total running time {:.3f}s'.format(total_running_time) else: output_str = 'Final acc: {:.3f}% | best acc {:.3f}% | total running time {:.3f}s'\ .format(val_acc*100, best_acc*100, total_running_time) print(output_str) if not config.screen_log_only: print(output_str, file=log_file) log_file.close()
i].loc[:, trainList[i].columns != target], testList[ i].loc[:, testList[i].columns != target] y_train, y_test = trainList[i][target], testList[i][target] """ Federico version train_df = trainList[i] test_df = testList[i] X_train = train_df.drop(columns=[target]) y_train = train_df[target] X_test = test_df.drop(columns=[target]) y_test = test_df[target] """ linear_regressor = util.getLinearClassifier() util.fitLinearRegressor(X_train, y_train, linear_regressor) prediction_linReg = util.predict(linear_regressor, X_test) mse_rmse_mae_linear = regressionErrors(y_test, prediction_linReg) print('*** Fold MSE, RMSE, MAE sklearn for linear regressor :', mse_rmse_mae_linear) prediction_linReg_all.extend(prediction_linReg) """ Using random forest regressor """ # X_train, X_test = trainList[i].loc[:, trainList[i].columns != target], testList[i].loc[:, testList[i].columns != target] # y_train, y_test = trainList[i][target], testList[i][target] rf_regressor = util.getRandomForestRegressor() util.fitLinearRegressor(X_train, y_train, rf_regressor) prediction_randomForest = util.predict(rf_regressor, X_test) mse_rmse_mae_rf = regressionErrors(y_test, prediction_randomForest) print( '*** Fold MSE, RMSE, MAE sklearn for random forest regressor :',
cat_to_name = build_cat_to_name(args.category_names) else: cat_to_name = build_cat_to_name() # Set the number of most likely classes to return if args.top_k != None: top_k = args.top_k else: top_k = 5 # Load model model = load_checkpoint(args.checkpoint) # Predict using the model probs, classes = predict(args.path_to_image, model, use_gpu=args.gpu, topk=top_k) # Format the predictions objects = [] for class_idx in np.array(classes).flatten(): for key, value in model.class_to_idx.items(): if class_idx == value: objects.append(cat_to_name[key]) y_pos = np.arange(len(objects)) performance = np.array(probs).flatten() # Print out the top_k highest probability flowers for flower, prob in zip(objects, performance): print('{} with probability: {}'.format(flower, prob))
mean_param = [v for v in tf.compat.v1.global_variables() if 'mean_tr:0' in v.name][0] label_enum_var = [v for v in tf.compat.v1.global_variables() if 'label_enum:0' in v.name][0] sess.run(tf.compat.v1.variables_initializer([mean_param, label_enum_var])) mean_tr = sess.run(mean_param) label_enum = sess.run(label_enum_var) test_batch, num_cls, _ = read_data(args.test_set, dim=args.dim, label_enum=label_enum) test_batch[0], _ = normalize_and_reshape(test_batch[0], dim=args.dim, mean_tr=mean_tr) x = tf.compat.v1.get_default_graph().get_tensor_by_name('main_params/input_of_net:0') y = tf.compat.v1.get_default_graph().get_tensor_by_name('main_params/labels:0') outputs = tf.compat.v1.get_default_graph().get_tensor_by_name('output_of_net:0') if args.loss == 'MSELoss': loss = tf.reduce_sum(input_tensor=tf.pow(outputs-y, 2)) else: loss = tf.reduce_sum(input_tensor=tf.nn.softmax_cross_entropy_with_logits(logits=outputs, labels=tf.stop_gradient(y))) network = (x, y, loss, outputs) avg_loss, avg_acc, results = predict(sess, network, test_batch, args.bsize) # convert results back to the original labels inverse_map = dict(zip(np.arange(num_cls), label_enum)) results = np.expand_dims(results, axis=1) results = np.apply_along_axis(lambda x: inverse_map[x[0]], axis=1, arr=results) print('In test phase, average loss: {:.3f} | average accuracy: {:.3f}%'.\ format(avg_loss, avg_acc*100))
parser = argparse.ArgumentParser() parser.add_argument('--image_path', action="store", dest="image_path") parser.add_argument('--checkpoint', action="store", dest="checkpoint") parser.add_argument('--top_k', action="store", default=5, type=int, dest="topk") parser.add_argument('--category_name', action="store", default=None, dest="cat") parser.add_argument('--gpu', action="store_true", default=False, dest="gpu") results = parser.parse_args() model, classes = utilities.load_checkpoint(results.checkpoint, results.gpu) if results.cat: classes_index = utilities.load_classes(results.cat) labels, probs, label_index = utilities.predict(results.image_path, model, classes, results.topk, classes_index, results.gpu) print('the label index of the image-the file name-', label_index) print('the name of the flower :', labels) print('the probabilities of each name: ', probs) ''' python predict.py --image_path 'flowers/test/79/image_06708.jpg' --checkpoint checkpointvgg.pth --category_name cat_to_name.json --top_k 5 --gpu '''
from utilities import predict, load_saved_model import nltk nltk.download('punkt') # load the model model = load_saved_model() while (1): try: input_sen = input( "Type a sentence to predict slots on! Or enter exit to quit.\n") except: input_sen = None if input_sen.lower() == 'exit': break if input_sen: predict(input_sen, model)
def main(): model_file = sys.argv[1] test_data_root = sys.argv[2] test_label_root = sys.argv[3] ## Load the config file (if necessary) _cfg = utilities.load_cfg_from_file('cfg_file.yml') # print _cfg ## Load all the data ## and convert the data to a unified Dict of eq classes print "Loading data .." print "Creating unified set of equivalence classes .." all_eq_classes = utilities.create_master_set(test_data_root) print "All equivalence classes created .." print "" # ## Make the Train dataframe print "Making the train dataframe with best features for multi-task prediction .." folder_names = utilities.get_folder_names(test_data_root) with open(_cfg.multi_features) as f: multi_features = pkl.load(f) train = pd.DataFrame(np.zeros( (len(folder_names), _cfg.cutoff) ), index=folder_names,columns=multi_features) # count =1 for feature in multi_features: # print count # count+=1 for folder in folder_names: if int(feature) in all_eq_classes[folder]: train.loc[folder,feature] = all_eq_classes[folder][int(feature)] # for folder in folder_names: # acc = all_eq_classes[folder] # classes = pd.Series(acc) # train.loc[folder] = classes train.fillna(0, inplace=True) # train = train.astype('int16') print "Train dataframe ready .." print "" # ## Some recycling # # del master_set # # del all_eq_c # # gc.collect() # ## Read the labels data print "Reading test labels .." labels = pd.read_csv(test_label_root, index_col=0) train = train.merge(labels, left_index=True, right_index=True) print "Reading Complete .." print "" ## Load the models with open(model_file) as f: model = pkl.load(f) with open(_cfg.all_scalers) as f: scalers = pkl.load(f) X = train.drop(['population', 'sequencing_center'], axis=1) Y = train[['population', 'sequencing_center']] with open(_cfg.binarizer) as f: mlb = pkl.load(f) y = mlb.transform(Y.as_matrix()) Y = pd.DataFrame(y) # X = scalers['multi_scaler'].transform(X) # ## Loading models from the files predict(X, Y, model['joint_model']) del X del Y del y del train gc.collect() ## Make the Train dataframe print "Making the train dataframe with best features for population prediction .." # folder_names = utilities.get_folder_names(test_data_root) with open(_cfg.pop_features) as f: pop_features = pkl.load(f) train = pd.DataFrame(np.zeros( (len(folder_names), _cfg.cutoff) ), index=folder_names,columns=pop_features) for feature in pop_features: for folder in folder_names: if int(feature) in all_eq_classes[folder]: train.loc[folder,feature] = all_eq_classes[folder][int(feature)] train.fillna(0, inplace=True) train = train.astype('int16') train = train.merge(labels, left_index=True, right_index=True) print "Train dataframe ready .." print "" X = train.drop(['population', 'sequencing_center'], axis=1) Y = train['population'] # X = scalers['pop_scaler'].transform(X) # mlb = MultiLabelBinarizer() # y = mlb.fit_transform(Y.as_matrix()) # Y = pd.DataFrame(y) ## Loading models from the files predict(X, Y, model['population']) del X del Y del train gc.collect() ## Make the Train dataframe print "Making the train dataframe with best features for sequencing center prediction .." # folder_names = utilities.get_folder_names(test_data_root) with open(_cfg.seq_features) as f: seq_features = pkl.load(f) train = pd.DataFrame(np.zeros( (len(folder_names), _cfg.cutoff) ), index=folder_names,columns=seq_features) for feature in seq_features: for folder in folder_names: if int(feature) in all_eq_classes[folder]: train.loc[folder,feature] = all_eq_classes[folder][int(feature)] train.fillna(0, inplace=True) train = train.astype('int16') train = train.merge(labels, left_index=True, right_index=True) print "Train dataframe ready .." print "" X = train.drop(['population', 'sequencing_center'], axis=1) Y = train['sequencing_center'] # X = scalers['seq_scaler'].transform(X) ## Loading models from the files predict(X, Y, model['sequencing_center'])
help="use a mapping of categories to real names") parser.add_argument('--gpu', action='store_true', default=False, dest="gpu", help="use gpu") # Capture command line arguments results = parser.parse_args() image_path = results.image checkpoint = results.checkpoint top_k = results.top_k category_names = results.category_names gpu = results.gpu # Load the provided checkpoint model = utilities.load_checkpoint(checkpoint) # Get the inverse of class_to_idx idx_to_class = {v: k for k, v in model.class_to_idx.items()} # Calculate top k probabilities and classes, using gpu if requested probs, classes = utilities.predict(image_path, checkpoint, idx_to_class, top_k, gpu) # Get label map if provided cat_to_name = utilities.label_mapping(category_names) # Print image class and top k class probabilities utilities.print_image_prob(image_path, cat_to_name, top_k, probs, classes)
def gradient_trainer(config, sess, network, full_batch, val_batch, saver, test_network): x, y, loss, outputs, = network global_step = tf.Variable(initial_value=0, trainable=False, name='global_step') learning_rate = tf.compat.v1.placeholder(tf.float32, shape=[], name='learning_rate') # Probably not a good way to add regularization. # Just to confirm the implementation is the same as MATLAB. reg = 0.0 param = tf.compat.v1.trainable_variables() for p in param: reg = reg + tf.reduce_sum(input_tensor=tf.pow(p, 2)) reg_const = 1 / (2 * config.C) batch_size = tf.compat.v1.cast(tf.shape(x)[0], tf.float32) loss_with_reg = reg_const * reg + loss / batch_size if config.optim == 'SGD': optimizer = tf.compat.v1.train.MomentumOptimizer( learning_rate=learning_rate, momentum=config.momentum).minimize(loss_with_reg, global_step=global_step) elif config.optim == 'Adam': optimizer = tf.compat.v1.train.AdamOptimizer( learning_rate=learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08).minimize(loss_with_reg, global_step=global_step) train_inputs, train_labels = full_batch num_data = train_labels.shape[0] num_iters = math.ceil(num_data / config.bsize) print(config.args) if not config.screen_log_only: log_file = open(config.log_file, 'w') print(config.args, file=log_file) sess.run(tf.compat.v1.global_variables_initializer()) print( '-------------- initializing network by methods in He et al. (2015) --------------' ) param = tf.compat.v1.trainable_variables() sess.run(init_model(param)) total_running_time = 0.0 best_acc = 0.0 lr = config.lr for epoch in range(0, args.epoch): loss_avg = 0.0 start = time.time() for i in range(num_iters): load_time = time.time() # randomly select the batch idx = np.random.choice(np.arange(0, num_data), size=config.bsize, replace=False) batch_input = train_inputs[idx] batch_labels = train_labels[idx] batch_input = np.ascontiguousarray(batch_input) batch_labels = np.ascontiguousarray(batch_labels) config.elapsed_time += time.time() - load_time step, _, batch_loss = sess.run( [global_step, optimizer, loss_with_reg], feed_dict={ x: batch_input, y: batch_labels, learning_rate: lr }) # print initial loss if epoch == 0 and i == 0: output_str = 'initial f (reg + avg. loss of 1st batch): {:.3f}'.format( batch_loss) print(output_str) if not config.screen_log_only: print(output_str, file=log_file) loss_avg = loss_avg + batch_loss # print log every 10% of the iterations if i % math.ceil(num_iters / 10) == 0: end = time.time() output_str = 'Epoch {}: {}/{} | loss {:.4f} | lr {:.6} | elapsed time {:.3f}'\ .format(epoch, i, num_iters, batch_loss , lr, end-start) print(output_str) if not config.screen_log_only: print(output_str, file=log_file) # adjust learning rate for SGD by inverse time decay if args.optim != 'Adam': lr = config.lr / (1 + args.lr_decay * step) # exclude data loading time for fair comparison epoch_end = time.time() - config.elapsed_time total_running_time += epoch_end - start config.elapsed_time = 0.0 if val_batch is None: output_str = 'In epoch {} train loss: {:.3f} | epoch time {:.3f}'\ .format(epoch, loss_avg/(i+1), epoch_end-start) else: if test_network == None: val_loss, val_acc, _ = predict(sess, network=(x, y, loss, outputs), test_batch=val_batch, bsize=config.bsize) else: # A separat test network part have been done... val_loss, val_acc, _ = predict(sess, network=test_network, test_batch=val_batch, bsize=config.bsize) output_str = 'In epoch {} train loss: {:.3f} | val loss: {:.3f} | val accuracy: {:.3f}% | epoch time {:.3f}'\ .format(epoch, loss_avg/(i+1), val_loss, val_acc*100, epoch_end-start) if val_acc > best_acc: best_acc = val_acc checkpoint_path = config.model_file save_path = saver.save(sess, checkpoint_path) print('Saved best model in {}'.format(save_path)) print(output_str) if not config.screen_log_only: print(output_str, file=log_file) if val_batch is None: checkpoint_path = config.model_file save_path = saver.save(sess, checkpoint_path) print('Model at the last iteration saved in {}\r\n'.format(save_path)) output_str = 'total running time {:.3f}s'.format(total_running_time) else: output_str = 'Final acc: {:.3f}% | best acc {:.3f}% | total running time {:.3f}s'\ .format(val_acc*100, best_acc*100, total_running_time) print(output_str) if not config.screen_log_only: print(output_str, file=log_file) log_file.close()
def newton(self, full_batch, val_batch, saver, network, test_network=None): """ Conduct newton steps for training args: full_batch & val_batch: provide training set and validation set. The function will save the best model evaluted on validation set for future prediction. network: a tuple contains (x, y, loss, outputs). test_network: a tuple similar to argument network. If you use layers which behave differently in test phase such as batchnorm, a separate test_network is needed. return: None """ # check whether data is valid full_inputs, full_labels = full_batch assert full_inputs.shape[0] == full_labels.shape[0] if full_inputs.shape[0] != self.config.num_data: raise ValueError('The number of full batch inputs does not agree with the config argument.\ This is important because global loss is averaged over those inputs') x, y, _, outputs = network tf.compat.v1.summary.scalar('loss', self.f) merged = tf.compat.v1.summary.merge_all() train_writer = tf.compat.v1.summary.FileWriter('./summary/train', self.sess.graph) print(self.config.args) if not self.config.screen_log_only: log_file = open(self.config.log_file, 'w') print(self.config.args, file=log_file) self.minibatch(full_batch, x, y, mode='fungrad') f = self.sess.run(self.f) output_str = 'initial f: {:.3f}'.format(f) print(output_str) if not self.config.screen_log_only: print(output_str, file=log_file) best_acc = 0.0 total_running_time = 0.0 self.config.elapsed_time = 0.0 total_CG = 0 for k in range(self.config.iter_max): # randomly select the batch for Gv estimation idx = np.random.choice(np.arange(0, full_labels.shape[0]), size=self.config.GNsize, replace=False) mini_inputs = full_inputs[idx] mini_labels = full_labels[idx] start = time.time() self.sess.run(self.init_cg_vars) cgtol = self.sess.run(self.cgtol) avg_cg_time = 0.0 for CGiter in range(1, self.config.CGmax+1): cg_time = time.time() self.minibatch((mini_inputs, mini_labels), x, y, mode='Gv') avg_cg_time += time.time() - cg_time self.sess.run(self.CG) rnewTrnew = self.sess.run(self.rTr) if rnewTrnew**0.5 <= cgtol or CGiter == self.config.CGmax: break self.sess.run(self.update_v) print('Avg time per Gv iteration: {:.5f} s\r\n'.format(avg_cg_time/CGiter)) gs, sGs = self.sess.run([self.update_gs, self.update_sGs], feed_dict={ self._lambda: self.config._lambda }) # line_search f_old = f alpha = 1 while True: old_alpha = 0 if alpha == 1 else alpha/0.5 self.sess.run(self.update_model, feed_dict={ self.alpha:alpha, self.old_alpha:old_alpha }) prered = alpha*gs + (alpha**2)*sGs self.minibatch(full_batch, x, y, mode='funonly') f = self.sess.run(self.f) actred = f - f_old if actred <= self.config.eta*alpha*gs: break alpha *= 0.5 # update lambda ratio = actred / prered if ratio < 0.25: self.config._lambda *= self.config.boost elif ratio >= 0.75: self.config._lambda *= self.config.drop self.minibatch(full_batch, x, y, mode='fungrad') f = self.sess.run(self.f) gnorm = self.sess.run(self.gnorm) summary = self.sess.run(merged) train_writer.add_summary(summary, k) # exclude data loading time for fair comparison end = time.time() end = end - self.config.elapsed_time total_running_time += end-start self.config.elapsed_time = 0.0 total_CG += CGiter output_str = '{}-iter f: {:.3f} |g|: {:.5f} alpha: {:.3e} ratio: {:.3f} lambda: {:.5f} #CG: {} actred: {:.5f} prered: {:.5f} time: {:.3f}'.\ format(k, f, gnorm, alpha, actred/prered, self.config._lambda, CGiter, actred, prered, end-start) print(output_str) if not self.config.screen_log_only: print(output_str, file=log_file) if val_batch is not None: # Evaluate the performance after every Newton Step if test_network == None: val_loss, val_acc, _ = predict( self.sess, network=(x, y, self.loss, outputs), test_batch=val_batch, bsize=self.config.bsize, ) else: # A separat test network part has not been done... val_loss, val_acc, _ = predict( self.sess, network=test_network, test_batch=val_batch, bsize=self.config.bsize ) output_str = '\r\n {}-iter val_acc: {:.3f}% val_loss {:.3f}\r\n'.\ format(k, val_acc*100, val_loss) print(output_str) if not self.config.screen_log_only: print(output_str, file=log_file) if val_acc > best_acc: best_acc = val_acc checkpoint_path = self.config.model_file save_path = saver.save(self.sess, checkpoint_path) print('Best model saved in {}\r\n'.format(save_path)) if val_batch is None: checkpoint_path = self.config.model_file save_path = saver.save(self.sess, checkpoint_path) print('Model at the last iteration saved in {}\r\n'.format(save_path)) output_str = 'total_#CG {} | total running time {:.3f}s'.format(total_CG, total_running_time) else: output_str = 'Final acc: {:.3f}% | best acc {:.3f}% | total_#CG {} | total running time {:.3f}s'.\ format(val_acc*100, best_acc*100, total_CG, total_running_time) print(output_str) if not self.config.screen_log_only: print(output_str, file=log_file) log_file.close()
cat_to_name = json.load(f) else: raise ValueError("Couldn't not identify the category names file.") # load the CNN from the checkpoint file if os.path.isfile(FLAGS.checkpoint_file): checkpoint_file = os.path.normpath(FLAGS.checkpoint_file) dnn_model, _ = utilities.load_checkpoint(checkpoint_file, device) else: raise ValueError("Couldn't not identify the checkpoint file.") # load image and do the inference if os.path.isfile(FLAGS.image_file): image_file = os.path.normpath(FLAGS.image_file) inference = utilities.predict(image_file, dnn_model, device, topk=FLAGS.top_k) else: raise ValueError("Couldn't not identify the image file.") # get real names of the infered classes if FLAGS.category_names != None: inference[1] = [cat_to_name[x] for x in inference[1]] print('Inference of the model loaded from checkpoint: ', checkpoint_file) for class_name, prob in zip(inference[1], inference[0]): print( 'The model predicts that the flower falls with {:.1f} percent certainty into class: {}' .format(prob * 100, class_name)) # print imput image along with the top top_k most probable classes
with open(category_names, "r") as f: cat_to_name = json.load(f) model_loaded = checkpoint_load(user_input.checkpoint) # model = getattr(models, "checkpoint.pth")(pretrained=True) image_processed = image_preprocess(prediction_image_dir) if gpu == True: image_processed = image_processed.to("cuda") else: pass # Make predictions probabilities, classes = predict(image_processed, model_loaded, topk, gpu) print(probabilities) print(classes) classes_names = [] for i in classes: classes_names += [cat_to_name[i]] print( "The flower in the photo you uploaded is most likely {} with the probability of {:.2f}%" .format(classes_names[0], 100 * probabilities[0])) print("Our second guess is {} with the probability of {:.2f}%".format( classes_names[1], 100 * probabilities[1])) print("And our third guess is {} with the probability of {:.2f}%".format( classes_names[2], 100 * probabilities[2]))