def save_video_to_milvus(video_path, video_name, table_name=const.MILVUS_KEYFRAME_TABLE): client = milvus_util.milvus_client() frames_path, duration = extract_frame(file_path=video_path, fps=5, video_name=video_name) print(frames_path) feats, names, urls = feature_extract_batch(database_path=frames_path, model=VGGNet()) duration_time = [re.split('[TF.]', time)[len(re.split('[TF.]', time)) - 3] for time in names] status, feats_ids = milvus_util.insert_vectors(client=client, table_name=table_name, vectors=feats) return status, feats_ids, urls, duration_time, duration
def run_validation(checkpoint_file, model_name): test_data, test_labels, _ = get_inputs_crop_flip('val') print("Retrieved validation data successfully") sample_size = get_size('val') keep_prob = 1. if model_name == 'AlexNet': model = AlexNetSmall(keep_prob) elif model_name == 'VGGNet': model = VGGNet(keep_prob) else: raise Exception('Not a valid model name') x = tf.placeholder(TYPE, shape=(BATCH_SIZE, IMAGE_FINAL_SIZE, IMAGE_FINAL_SIZE, NUM_CHANNELS), name='input') logits, variables = model.model(x) prediction = tf.nn.softmax(logits) batch_data, batch_labels = tf.train.batch( [test_data, test_labels], batch_size=BATCH_SIZE, capacity=5*BATCH_SIZE ) if USE_GPU: config = tf.ConfigProto() else: config = tf.ConfigProto(device_count={'GPU': 0}) saver = tf.train.Saver() with tf.Session(config=config) as sess: # Restore variables saver.restore(sess=sess, save_path=checkpoint_file) print("Restored variables from checkpoint %s" % checkpoint_file) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) predictions = [] labels = [] for step in range(sample_size // BATCH_SIZE): _data, _labels = sess.run([batch_data, batch_labels]) _data = np.swapaxes(_data,0,1) single_prediction = [] for i in range(8): test_feed_dict = {x: _data[i]} test_predictions = sess.run(prediction, feed_dict=test_feed_dict) single_prediction.append(test_predictions) # single_prediction is 8 * BATCH_SIZE * NUM_CAT single_prediction = np.array(single_prediction) single_prediction = np.swapaxes(single_prediction, 0, 1) #single prediction is now BATCH_SIZE * 8 * NUM_CAT # _labels is BATCH_SIZE labels.extend(_labels) predictions.extend(single_prediction.tolist()) # NUM_IMAGES * 8 * NUM_CAT print('Processing batch number: %d / %d' % (step, sample_size//BATCH_SIZE)) # Save predictions in pickle file filename = checkpoint_file.split('/') if filename[-1] == '': prefix = filename[-2] else: prefix = filename[-1] prediction_file = PREDICTIONS_DIR + 'val_prediction__' + prefix label_file = PREDICTIONS_DIR + 'val_labels__' + prefix with open(prediction_file, 'wb') as f: pickle.dump(predictions, f) with open(label_file, 'wb') as f: pickle.dump(labels, f) print('Created new pickle file with predictions in %s' % prediction_file) coord.request_stop() coord.join(threads) return (prediction_file, label_file)
def run_test(checkpoint_file, model_name): # Get test data test_data, test_labels, _ = get_inputs_crop_flip('test') print("Retrieved test data successfully") sample_size = get_size('test') # Initialize model keep_prob = 1. if model_name == 'AlexNet': model = AlexNetSmall(keep_prob) elif model_name == 'VGGNet': model = VGGNet(keep_prob) else: raise Exception('Not a valid model name') x = tf.placeholder(TYPE, shape=(BATCH_SIZE, IMAGE_FINAL_SIZE, IMAGE_FINAL_SIZE, NUM_CHANNELS), name='input') logits, variables = model.model(x) prediction = tf.nn.softmax(logits) batch_data = tf.train.batch( test_data, batch_size=BATCH_SIZE, capacity=5*BATCH_SIZE) if USE_GPU: config = tf.ConfigProto() else: config = tf.ConfigProto(device_count={'GPU': 0}) saver = tf.train.Saver() with tf.Session(config=config) as sess: # Restore variables saver.restore(sess=sess, save_path=checkpoint_file) print("Restored variables from checkpoint %s" % checkpoint_file) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) predictions = [] for step in range(sample_size // BATCH_SIZE): _data = sess.run(batch_data) single_prediction = [] for i in range(8): test_feed_dict = {x: _data[i]} test_predictions = sess.run(prediction, feed_dict=test_feed_dict) single_prediction.append(test_predictions) # single_prediction is 8 * BATCH_SIZE * NUM_CAT single_prediction = np.array(single_prediction) single_prediction = np.swapaxes(single_prediction, 0, 1) #single prediction is now BATCH_SIZE * 8 * NUM_CAT # average_prediction = np.prod(np.array(single_prediction), axis=0) # flat_prediction = np.array(single_prediction).flatten() # ind = np.argpartition(flat_prediction, -40)[-40:] # indices = ind[np.argsort(flat_prediction[ind])][::-1] # indices = np.unique(indices%100)[:5] # average_prediction = np.zeros(100) # for i in range(5): # average_prediction[indices[i]] = 6-i predictions.extend(single_prediction.tolist()) # NUM_IMAGES * 8 * NUM_CAT print('Processing batch number: %d / %d' % (step, sample_size//BATCH_SIZE)) # Save predictions in pickle file filename = checkpoint_file.split('/') if filename[-1] == '': prefix = filename[-2] else: prefix = filename[-1] prediction_file = PREDICTIONS_DIR + 'prediction__' + prefix with open(prediction_file, 'wb') as f: pickle.dump(predictions, f) print('Created new pickle file with predictions in %s' % prediction_file) coord.request_stop() coord.join(threads) return prediction_file
(checkpoint_prefix + "-" + str(step))) saver.save(sess, checkpoint_prefix, global_step=step, write_meta_graph=False) print("\tSuccess!\n") coord.request_stop() coord.join(threads) if __name__ == '__main__': # optimizer = tf.train.AdamOptimizer(0.001) optimizer = tf.train.AdagradOptimizer(0.01) target_categories = [] # target_categories = ['playground', 'abbey', 'amphitheater', 'baseball_field', 'bedroom', 'cemetery', 'courtyard', 'kitchen', 'mountain', 'shower'] # target_categories = ALL_CATEGORIES[:10] keep_prob = tf.placeholder( tf.float32, name='keep_prob') # we need to define a probability for the dropout ### Example when running BrianNet #run(target_categories, optimizer, {}, {}, model=BrianNet()) # model = AlexNetSmall(keep_prob) model = VGGNet(keep_prob) run(target_categories, optimizer, {keep_prob: 1.}, {keep_prob: KEEP_PROB}, model=model)
device = args.gpu n_epoch = 81 # 保存先をチェックする if os.path.exists('models/' + args.dir): print('selected dir exists!') else: print('selected dir does not exits! make dir.') os.mkdir('models/' + args.dir) # 各種必要なパラメータを読み込み # VGGmodelを読み込む if args.usevgg == 1: print('loading VGG model...') vgg = VGGNet() serializers.load_hdf5('/tmp/VGG.model', vgg) print('loaded VGG!') # ############学習の高速化のためにパラメータを事前に整理しておく################### if args.usevgg == 1: print('preprocessing vgg data') vgg_img_param = [] start = time.time() if os.path.isfile('features/vggparam_yahoo.pickle'): print('vgg checkpoint pickle is exist! loading pickle') with open('features/vggparam_yahoo.pickle', mode='rb') as f: vgg_img_param = pickle.load(f) else:
text, prob = classifier(img_name) print("------------------------------") print(f"Label: {text:<75}") print(f"Probability: {prob:.6f}.") print("------------------------------") if args.echo: self.echo(str(text), prob) def echo(self, text, prob): QMessageBox.information( self, "Message", f"Label :{str(text):<75}\nProbability: {prob:.6f}") if __name__ == "__main__": model = VGGNet.from_pretrained(args.model_name) model.eval() if args.image_size is None: args.image_size = VGGNet.get_image_size(args.model_name) # Preprocess image tfms = transforms.Compose([ transforms.Resize(args.image_size), transforms.CenterCrop(args.image_size), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), ]) # Load class names labels_map = json.load(open(args.labels_map)) labels_map = [labels_map[str(i)] for i in range(args.num_classes)]
# Open image img = Image.open('panda.jpg') # Preprocess image tfms = transforms.Compose([ transforms.Resize(image_size), transforms.CenterCrop(image_size), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), ]) img = tfms(img).unsqueeze(0) # Load class names labels_map = json.load(open('labels_map.txt')) labels_map = [labels_map[str(i)] for i in range(1000)] # Classify with AlexNet print("=> loading checkpoint 'vggnet-b11'.") model = VGGNet.from_pretrained('vggnet-b11') print("=> loaded checkpoint 'vggnet-b11'.") model.eval() with torch.no_grad(): logits = model(img) preds = torch.topk(logits, k=5).indices.squeeze(0).tolist() print('-----') for idx in preds: label = labels_map[idx] prob = torch.softmax(logits, dim=1)[0, idx].item() print('{:<75} ({:.2f}%)'.format(label, prob * 100))
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' from vggnet import VGGNet # Integer value represents output channel after performing the convolution layer # 'M' represents the max pooling layer # After convolution blocks; flatten the output and use 4096x4096x1000 Linear Layers # with soft-max at the end VGG_types = { 'VGG11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], 'VGG13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], 'VGG16': [ 64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M' ], 'VGG19': [ 64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M' ], } if __name__ == "__main__": # test VGGNet16 model = VGGNet(name="VGGNet16", architecture=VGG_types["VGG16"], input_shape=(224, 224, 3), classes=1000) model.summary()
# import argparse # from PIL import ImageFile from chainer import cuda, serializers, Variable from tinynet import wordQueryNet from tinynet_novgg import wordQueryNetNoVGG from wordparam import word2vector from vggparam import vggparamater from vggnet import VGGNet import pickle import argparse import cv2 as cv # import chainer # import chainer.functions as F vgg = VGGNet() serializers.load_hdf5('/tmp/VGG.model', vgg) mean = np.array([103.939, 116.779, 123.68]) img = cv.imread(image).astype(np.float32) img -= mean img = cv.resize(img, (224, 224)).transpose((2, 0, 1)) img = img[np.newaxis, :, :, :] gpu = 0 if gpu >= 0: cuda.get_device(gpu).use() vgg.to_gpu() img = cuda.cupy.asarray(img, dtype=np.float32) pred = vgg(Variable(img), None)
def main_worker(gpu, ngpus_per_node, args): global best_acc1 args.gpu = gpu if args.gpu is not None: print("Use GPU: {} for training".format(args.gpu)) if args.distributed: if args.dist_url == "env://" and args.rank == -1: args.rank = int(os.environ["RANK"]) if args.multiprocessing_distributed: # For multiprocessing distributed training, rank needs to be the # global rank among all the processes args.rank = args.rank * ngpus_per_node + gpu dist.init_process_group(backend=args.dist_backend, init_method=args.dist_url, world_size=args.world_size, rank=args.rank) # create model if 'vgg' in args.arch: # NEW if args.pretrained: model = VGGNet.from_pretrained(args.arch, num_classes=args.num_classes) print("=> using pre-trained model '{}'".format(args.arch)) else: print("=> creating model '{}'".format(args.arch)) model = VGGNet.from_name(args.arch) else: if args.pretrained: print("=> using pre-trained model '{}'".format(args.arch)) model = models.__dict__[args.arch](pretrained=True) else: print("=> creating model '{}'".format(args.arch)) model = models.__dict__[args.arch]() if args.distributed: # For multiprocessing distributed, DistributedDataParallel constructor # should always set the single device scope, otherwise, # DistributedDataParallel will use all available devices. if args.gpu is not None: torch.cuda.set_device(args.gpu) model.cuda(args.gpu) # When using a single GPU per process and per # DistributedDataParallel, we need to divide the batch size # ourselves based on the total number of GPUs we have args.batch_size = int(args.batch_size / ngpus_per_node) args.workers = int(args.workers / ngpus_per_node) model = torch.nn.parallel.DistributedDataParallel( model, device_ids=[args.gpu]) else: model.cuda() # DistributedDataParallel will divide and allocate batch_size to all # available GPUs if device_ids are not set model = torch.nn.parallel.DistributedDataParallel(model) elif args.gpu is not None: torch.cuda.set_device(args.gpu) model = model.cuda(args.gpu) else: # DataParallel will divide and allocate batch_size to all available GPUs if args.arch.startswith('alexnet') or args.arch.startswith('vgg'): model.features = torch.nn.DataParallel(model.features) model.cuda() else: model = torch.nn.DataParallel(model).cuda() # define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss().cuda(args.gpu) optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay) model, optimizer = amp.initialize(model, optimizer, opt_level=args.opt_level) # optionally resume from a checkpoint if args.resume: if os.path.isfile(args.resume): print("=> loading checkpoint '{}'".format(args.resume)) checkpoint = torch.load(args.resume) args.start_epoch = checkpoint['epoch'] best_acc1 = checkpoint['best_acc1'] if args.gpu is not None: # best_acc1 may be from a checkpoint from a different GPU best_acc1 = best_acc1.to(args.gpu) model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) amp.load_state_dict(checkpoint['amp']) print("=> loaded checkpoint '{}' (epoch {})".format( args.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(args.resume)) cudnn.benchmark = True # Data loading code normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) train_dataset = datasets.CIFAR10(root=args.data, train=True, download=True, transform=transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize, ])) if args.distributed: train_sampler = torch.utils.data.distributed.DistributedSampler( train_dataset) else: train_sampler = None train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=(train_sampler is None), num_workers=args.workers, pin_memory=True, sampler=train_sampler) test_dataset = datasets.CIFAR10(root=args.data, train=False, download=True, transform=transforms.Compose([ transforms.ToTensor(), normalize, ])) test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=True) if args.evaluate: top1, top5 = validate(test_loader, model, criterion, args) with open("res.txt", "w") as f: print(f"Acc@1: {top1}\tAcc@5: {top5}", file=f) return for epoch in range(args.start_epoch, args.epochs): if args.distributed: train_sampler.set_epoch(epoch) adjust_learning_rate(optimizer, epoch, args) # train for one epoch train(train_loader, model, criterion, optimizer, epoch, args) # evaluate on validation set acc1, _ = validate(test_loader, model, criterion, args) # remember best acc@1 and save checkpoint is_best = acc1 > best_acc1 best_acc1 = max(acc1, best_acc1) if not args.multiprocessing_distributed or ( args.multiprocessing_distributed and args.rank % ngpus_per_node == 0): save_checkpoint( { 'epoch': epoch + 1, 'arch': args.arch, 'state_dict': model.state_dict(), 'best_acc1': best_acc1, 'optimizer': optimizer.state_dict(), 'amp': amp.state_dict(), }, is_best)
def save_feats_batch_to_milvus(keyframe_path, table_name): client = milvus_util.milvus_client() feats, names, urls= feature_extract(database_path=keyframe_path, model=VGGNet()) status, feats_ids = milvus_util.insert_vectors(client=client, table_name=table_name, vectors=feats) return status, feats_ids, names
256, 256, 'M', # Layer 3 512, 512, 512, 512, 'M', # Layer 4 512, 512, 512, 512, 'M', # Layer 5 ] if __name__ == "__main__": opt = Adam(lr=0.001) # test VGGNet16 model = VGGNet(name="VGGNet16", architecture=VGG16, input_shape=(224, 224, 3), classes=1000) model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) X_train = np.random.randn(1080, 224, 224, 3).astype('f') Y_train = np.random.randn(1080, 1000).astype('f') model.fit(X_train, Y_train, epochs=1, batch_size=32)