def load_data(width): X_TRAIN = load_images(width, kind='train') X_TEST = load_images(width, kind='test') M_TRAIN, S_TRAIN = pickled(f'm_s_{width}.p', get_mean_std, X_TRAIN) W_ZCA = pickled(f'zca_{width}.p', get_whitening_matrix, X_TRAIN, M_TRAIN) return X_TRAIN, X_TEST, M_TRAIN, W_ZCA
def get_data_loader(name, train=True): print("use dataset: {}".format(name)) if name == "MNIST": return get_mnist(train) elif name == "USPS": return get_usps(train) elif name == "SVHN": return get_svhn(train) elif name == "A": return load_images('data/office/', 'amazon', batch_size=config.batch_size, is_train=train) elif name == "W": return load_images('data/office/', 'webcam', batch_size=config.batch_size, is_train=train) elif name == "D": return load_images('data/office/', 'dslr', batch_size=config.batch_size, is_train=train) elif name == "B": return load('data/image-clef/b_list.txt', batch_size=config.batch_size, is_train=train) elif name == "C": return load('data/image-clef/c_list.txt', batch_size=config.batch_size, is_train=train) elif name == "I": return load('data/image-clef/i_list.txt', batch_size=config.batch_size, is_train=train) elif name == "P": return load('data/image-clef/p_list.txt', batch_size=config.batch_size, is_train=train)
def main(arg_list=None): # Initialize parser = get_parser() if arg_list: # Called from another script args = parser.parse_args(arg_list) else: # Called from command line args = parser.parse_args() path_image = args.path_image path_model = args.path_model size_of_image = args.size_of_image if args.size_of_image else SIZE_OF_IMAGE path_json = args.path_json if args.path_json else PATH_JSON top_k = args.top_k if args.top_k else 1 # Load and preprocess images X, file_names = preprocess.load_images(path_image, size_of_image=size_of_image) print(X) X = preprocess.preprocess(X, size_of_image=size_of_image) # Load trained model model = models.load_model(path_model) # model.summary() # Load label_name from .json with open(path_json, 'r') as f: label_name = json.load(f) # Make predictions predictions_int = model.predict(X) print(predictions_int) prediction_name = [ ] # Stores top-k predicted pokemon names of the input images for prediction in predictions_int: # Find indices with top-k highest values (numpy array: [highest, ..., lowest]) top_k_index = np.argsort(prediction)[::-1][:top_k] prediction_name.append( [label_name[str(index.item())] for index in top_k_index]) for file, prediction in zip(file_names, prediction_name): print("Below are the top {} likely Pokemon in {}".format(top_k, file)) print(prediction)
def main(): # constants batch_size = cfg.config['batch_size'] num_channels = cfg.config['num_channels'] classes = cfg.config['classes'] num_classes = len(classes) height = cfg.config['height'] width = cfg.config['width'] anchors = cfg.config['anchors'] num_anchors = len(anchors[0]) num_epochs = cfg.config['num_epochs'] learning_rate = cfg.config['learning_rate'] weight_decay = cfg.config['weight_decay'] cuda = cfg.config['CUDA'] image_dir = cfg.config['path']['image'] target_dir = cfg.config['path']['train'] weight_dir = cfg.config['path']['weight'] initial_weight_dir = cfg.config['path']['initial_weight'] image_paths = pre.load_image_paths(image_dir, target_dir) target_paths = pre.load_dir_paths(target_dir) num_images = len(image_paths) now = datetime.datetime.now() now = now.strftime('%Y-%m-%d_%H-%M-%S') weight_file = False # network net = model.YOLOv3(num_channels, num_classes, num_anchors) if cuda: net = net.cuda() net = net.train(True) if initial_weight_dir: net.load_state_dict(torch.load(initial_weight_dir)) print('Load initial weight from {}.'.format(initial_weight_dir)) # optimizer optimizer = optim.AdamW(net.parameters(), lr=learning_rate, weight_decay=weight_decay) # train for epoch in range(num_epochs): losses_giou = [] losses_obj = [] losses_prob = [] losses_blc = [] t0 = time.time() for i in range((num_images // batch_size) + 1): # get indices start = batch_size * i end = min(start + batch_size, num_images) if start >= end: break # load images and targets images = pre.load_images(image_paths[start:end], height, width, cuda) targets = pre.load_targets(target_paths[start:end], num_classes, height, width, cuda) # train loss_giou, loss_obj, loss_cls, loss_blc = train( net, optimizer, images, targets, anchors, height, width, cuda) # NaN if np.isnan(loss_giou + loss_obj + loss_cls + loss_blc): print( "NaN is occured. Loss: {:.2f} {:.2f} {:.2f} {:.2f}".format( loss_giou, loss_obj, loss_cls, loss_blc)) if weight_file: net.load_state_dict(torch.load(weight_file)) optimizer = optim.AdamW(net.parameters(), lr=learning_rate, weight_decay=weight_decay) print("Reset weight to {}.".format(weight_file)) break else: print("Previous weight does not exist.") break losses_giou.append(loss_giou) losses_obj.append(loss_obj) losses_prob.append(loss_cls) losses_blc.append(loss_blc) # NaN if np.isnan(loss_giou + loss_obj + loss_cls): continue # calculate average of loss loss_giou = sum(losses_giou) / len(losses_giou) loss_obj = sum(losses_obj) / len(losses_obj) loss_cls = sum(losses_prob) / len(losses_prob) loss_blc = sum(losses_blc) / len(losses_blc) loss = loss_giou + loss_obj + loss_cls + loss_blc # time elapse elapsed_time = time.time() - t0 print(("Epoch: {}, Elapsed Time: {:.2f}s, " + "GIoU Loss: {:.2f}, " + "Objectness Loss: {:.2f}, " + "Class Loss: {:.2f}, " + "Balance Loss: {:.2f}, " + "Loss: {:.2f}").format( epoch, elapsed_time, loss_giou, loss_obj, loss_cls, loss_blc, loss)) # save weight text = "{}_{:0>4}_{:.2f}.pt".format(now, epoch, loss) weight_file = os.path.join(weight_dir, text) torch.save(net.state_dict(), weight_file) print("Saved {}.".format(weight_file))
from train import train, test from preprocess import load_images, process_image def get_label(pred_index): if pred_index <= 9: return chr(pred_index + 48) else: return chr(pred_index + 87) if not (os.path.exists('./gesture-model.h5')): print("Training new model") x_train, x_val, x_test, y_train, y_val, y_test = load_images() train(x_train=x_train, y_train=y_train, x_val=x_val, y_val=y_val) test(x_test=x_test, y_test=y_test) model = load_model('gesture-model.h5') predicted_text = "" cap = cv2.VideoCapture(0) left_margin = 100 while True: ret, frame = cap.read() frame = cv2.flip(frame, 1) cv2.rectangle(frame, (350, 150), (600, 400), (0, 255, 0)) hand_frame = frame[150:400, 350:600]
import classification_net as cn from activation_methods import * import preprocess as p if __name__ == "__main__": train, target = p.load_images('train.csv') train, target, test, target_test = p.partition_set(.8, train, target) net = cn.ClassificationNetwork([train.shape[1], 128, 64, 10], Activations.sigmoid) net.train_network(train, target, epochs=100, reg_strength=0.001, learning_rate=0.00001, batch_size=100, momentum=0.95, debug=False) print(net.compute_accuracy(test, target_test))