def _prepare_base_model(self, base_model): if 'squeezenet' in base_model: self.base_model = getattr(torchvision.models, base_model)(True) if base_model == 'squeezenet1_1': self.base_model = self.base_model.features self.base_model.last_layer_name = '12' else: self.base_model.last_layer_name = 'fc' self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] elif 'mobilenetv2' in base_model: import mobilenetv2 self.base_model = mobilenetv2.mobilenetv2(input_size=224, width_mult=1.) self.base_model.load_state_dict( torch.load('pretrained_models/mobilenetv2-0c6065bc.pth')) self.base_model.last_layer_name = 'classifier' self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] else: raise ValueError('Unknown base model: {}'.format(base_model))
def __init__(self, opt): super(Mobilenetv2Classification, self).__init__(opt) # Initialize network self.model = mobilenetv2(pretrained=True) self.model.classifier = nn.Linear(in_features=1280, out_features=2)
def build_and_model(): g, img_tensor, input_boxes, detection_boxes, detection_scores = counter_v2.build_model( apply_and_model=True) with g.as_default(): clip_boxes = tf.stack([detection_boxes[:, 1], detection_boxes[:, 0], \ detection_boxes[:, 3], detection_boxes[:, 2]], 1) canimg_batch = crop_and_resize(img_tensor, clip_boxes, 64) _, cls_scores_with_background, _ = mobilenetv2(canimg_batch, num_classes=2, wid=3, is_train=False) cls_scores = tf.slice(cls_scores_with_background, [0, 1], [-1, -1]) cls_scores = tf.squeeze(cls_scores, 1) positive_indices = tf.equal(tf.argmax(cls_scores_with_background, 1), 1) positive_indices = tf.squeeze(tf.where(positive_indices), 1) cls_boxes = tf.gather(detection_boxes, positive_indices) cls_scores = tf.gather(cls_scores, positive_indices) averange_scores = tf.truediv(tf.add(detection_scores, cls_scores), 2.0) num_batch = shape_utils.combined_static_and_dynamic_shape(input_boxes) input_scores = tf.tile([0.7], [num_batch[0]]) total_boxes = tf.concat([cls_boxes, input_boxes], 0) total_scores = tf.concat([averange_scores, input_scores], 0) result_dict = non_max_suppression( total_boxes, total_scores, iou_threshold=0.03, scope='And_model/Non_max_suppression') output_node_names = [ 'And_model/Non_max_suppression/result_boxes', 'And_model/Non_max_suppression/result_scores', 'And_model/Non_max_suppression/abnormal_indices' ] init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) # saver for restore model saver_det = get_checkpoint_init_fn( '../checkpoints/counter_v2/model.ckpt-150000', exclude_var=['mobilenetv2']) saver_cls = get_checkpoint_init_fn( '../checkpoints/64x64-128-0.95-0.001-wid3/mobilenetv2-17', include_var=['mobilenetv2']) saver_det(sess) saver_cls(sess) write_pb_model('../checkpoints/and_model.pb', sess, g.as_graph_def(), output_node_names)
def deeplabv3(inputs, input_shape, num_classes, output_stride, backbone="mobilenet"): if backbone == "mobilenet": lowlevel, x = mobilenetv2(inputs) else: raise ValueError(f"{backbone} is not integrated yet.") x = aspp(x, output_stride) x = decoder(x, lowlevel, num_classes) h, w = inputs.shape[1] // x.shape[1], inputs.shape[2] // x.shape[2] x = tf.keras.layers.UpSampling2D(size=(h, w), interpolation="bilinear")(x) return x
def export(build_dir, output_file, output_node, input_height, input_width, input_chan): tf.keras.backend.set_learning_phase(0) model = mobilenetv2(input_shape=(input_height, input_width, input_chan), classes=2, alpha=1.0, incl_softmax=False) model.compile(loss=SparseCategoricalCrossentropy(from_logits=True)) graph_def = tf.compat.v1.keras.backend.get_session().graph.as_graph_def() graph_def = tf.compat.v1.graph_util.extract_sub_graph( graph_def, [output_node]) tf.io.write_graph(graph_def, build_dir, output_file, as_text=True) return
def training_net(): image,label,mask,isTraining = net_placeholder(None) logits = mobilenetv2(image,isTraining) loss = net_loss(label,mask,logits) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_step = tf.train.AdamOptimizer(parameters.LEARNING_RATE).minimize(loss) Saver = tf.train.Saver(var_list=tf.global_variables(),max_to_keep=5) with tf.Session() as sess: writer = tf.summary.FileWriter(os.path.join(parameters.path,'model'), sess.graph) init_var_op = tf.global_variables_initializer() sess.run(init_var_op) for i in range(parameters.TRAIN_STEPS): batch = mini_batch(i,parameters.BATCH_SIZE,'train') feed_dict = {image:batch['image'], label:batch['label'], mask:batch['mask'], isTraining:True} _,loss_ = sess.run([train_step,loss],feed_dict=feed_dict) print('===>Step %d: loss = %g ' % (i,loss_)) # evaluate and save checkpoint if i % 500 == 0: write_instance_dir = os.path.join(parameters.path,'pic') if not os.path.exists(write_instance_dir):os.mkdir(write_instance_dir) j = 0 while True: batch = mini_batch(j,1,'val') feed_dict = {image:batch['image'],isTraining:False} pred_output = sess.run(logits,feed_dict=feed_dict) pred_output = np.squeeze(pred_output) pred_output = box_decode(pred_output,batch['image_name'],parameters.SCORE_THRESHOLD) pred_output = nms(pred_output,parameters.NMS_THRESHOLD) if j < min(20,batch['image_num']):save_instance(pred_output) if j == batch['image_num']-1:break j += 1 if os.path.exists(parameters.CHECKPOINT_MODEL_SAVE_PATH) and (i==0): shutil.rmtree(parameters.CHECKPOINT_MODEL_SAVE_PATH) Saver.save(sess,os.path.join(parameters.CHECKPOINT_MODEL_SAVE_PATH,parameters.MODEL_NAME))
def _prepare_base_model(self, base_model): if 'resnet' in base_model or 'resnext' in base_model: self.base_model = getattr(senet, base_model)(num_classes=1000, pretrained='imagenet') self.base_model.last_layer_name = 'last_linear' self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] elif 'mobilenet' in base_model: from mobilenetv2 import mobilenetv2 self.base_model = mobilenetv2() self.base_model.last_layer_name = 'last_linear' self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] else: raise ValueError('Unknown base model: {}'.format(base_model))
def main(): errfile = '../_error/other_errors.txt' num_samples = 14292 sess = tf.Session() # read queue glob_pattern = os.path.join(args.dataset_dir, 'other.tfrecords') #tfrecords_list = glob.glob(glob_pattern) img_batch, label_batch, name_batch = get_batch( glob_pattern, args.batch_size, epochs=1, shuffle=False) #inputs = tf.placeholder(tf.float32, [None, height, width, 3], name='input') _, pred, _ = mobilenetv2(img_batch, wid=args.wid, num_classes=args.num_classes, is_train=False) # evaluate model, for classification correct_pred = tf.equal(tf.argmax(pred, 1), tf.cast(label_batch, tf.int64)) acc = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) # saver for restore model saver = tf.train.Saver() print('[*] Try to load trained model...') ckpt_name = load(sess, saver, args.checkpoint_dir) step = 0 accs = 0 me_acc = 0 errors_name = [] max_steps = int(num_samples / args.batch_size) print('START TESTING...') try: while not coord.should_stop(): for _step in range(step+1, step+max_steps+1): # test _name, _corr, _acc = sess.run([name_batch, correct_pred, acc]) if (~_corr).any(): errors_name.extend(list(_name[~_corr])) accs += _acc me_acc = accs/_step if _step % 20 == 0: print(time.strftime("%X"), 'global_step:{0}, current_acc:{1:.6f}'.format (_step, me_acc)) except tf.errors.OutOfRangeError: accuracy = 1 - len(errors_name)/num_samples print(time.strftime("%X"), 'RESULT >>> current_acc:{0:.6f}'.format(accuracy)) # print(errors_name) errorsfile = open(errfile, 'a') errorsfile.writelines('\n' + ckpt_name + '--' + str(accuracy)) for err in errors_name: errorsfile.writelines('\n' + err.decode('utf-8')) errorsfile.close() finally: coord.request_stop() coord.join(threads) sess.close() print('FINISHED TESTING.')
import os import codecs, json from time import strftime, time import cv2 import numpy as np from CapDetection import CapDetection from mobilenetv2 import mobilenetv2 from ops import * if __name__ == '__main__': # bulid model inputs = tf.placeholder(tf.float32, [None, 64, 64, 3], name='input') _, pred, _ = mobilenetv2(inputs, wid=3, num_classes=2, is_train=False) y = tf.argmax(pred, 1) ckpt = '../checkpoints/64x64-128-0.95-0.001-wid3/model' sess = tf.Session() # saver for restore model saver = tf.train.Saver() saver.restore(sess, ckpt) img_dir = '/media/jun/data/capdataset/detection/test/' result_dir = '/media/jun/data/capdataset/detection/result_test/' cirfile = '/media/jun/data/capdataset/detection/result_test.json' cap_types = [(6, 10), (5, 8), (6, 10), (6, 10), (5, 10)] boxes = CapDetection(img_dir, result_dir) result_boxes = {} for img_name in boxes.imgs: t = int(img_name[0])
tfrec_dir = 'data/tfrecords' batchsize = 50 # simple generator to provide incrementing integer def step_number(num=0): while True: num += 1 yield num step_num = step_number() # model to be evaluated eval_model = mobilenetv2(input_shape=(input_height, input_width, input_chan), classes=2, alpha=1.0, incl_softmax=False) eval_model.compile(loss=SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) # test dataset test_dataset = input_fn_test(tfrec_dir, batchsize) # eval function def evaluate(checkpoint_path=''): eval_model.load_weights(checkpoint_path) scores = eval_model.evaluate(test_dataset) eval_metric_ops = {'accuracy': scores[1]} print('*** Accuracy after', next(step_num), 'steps:', scores[-1], '***')
def __init__(self, opt): super(Mobilenetv2, self).__init__(opt) # Initialize network self.model = mobilenetv2(pretrained=True)
def restore_params(): # 新建 net3 net2 = mobilenetv2.mobilenetv2(num_classes=2) # 将保存的参数复制到 net3 net2.load_state_dict(torch.load('net_params.pkl')) return net2
parser = argparse.ArgumentParser(description='PyTorch Fer2013 CNN Training') parser.add_argument('--model', type=str, default='mobileNet_V1', help='CNN architecture') opt = parser.parse_args() use_cuda = torch.cuda.is_available() # Model if opt.model == 'mobileNet_V1': net = mobilenet(num_classes=7) elif opt.model == 'mobileNet_05': net = mobilenet_05(num_classes=7) elif opt.model == 'mobileResNet_v1': net = mobileResnet(num_classes=7) elif opt.model == 'mobilenetv2': net = mobilenetv2(num_classes=7, input_size=96) checkpoint = torch.load( os.path.join('FER2013_mobileNet_V1/model_acc88.30', 'PrivateTest_model.t7')) net.load_state_dict(checkpoint['net']) if use_cuda: net = net.cuda() cut_size = 90 CASC_PATH = 'E:/opencv/build/etc/haarcascades/haarcascade_frontalface_default.xml' LBP_PATH = 'E:/opencv/build/etc/lbpcascadeslbpcascade_frontalcatface.xml' cascade_classifier = cv2.CascadeClassifier(CASC_PATH) lbp_classifier = cv2.CascadeClassifier(LBP_PATH) class_names = [ 'Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral'
def main(): #height = args.height #width = args.width sess = tf.Session() # read queue glob_pattern = os.path.join(args.dataset_dir, 'train.tfrecords') #tfrecords_list = glob.glob(glob_pattern) img_batch, label_batch, _ = get_batch(glob_pattern, args.batch_size, args.epoch) # print(img_batch) #inputs = tf.placeholder(tf.float32, [None, height, width, 3], name='input') logits, pred, _ = mobilenetv2(img_batch, wid=args.wid, num_classes=args.num_classes, is_train=args.is_train) # loss loss_ = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits(labels=label_batch, logits=logits)) # L2 regularization l2_loss = tf.add_n(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)) total_loss = loss_ + l2_loss # evaluate model, for classification correct_pred = tf.equal(tf.argmax(pred, 1), tf.cast(label_batch, tf.int64)) acc = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # learning rate decay base_lr = tf.constant(args.learning_rate) lr_decay_step = args.num_samples // args.batch_size * 2 # every epoch global_step = tf.placeholder(dtype=tf.float32, shape=()) lr = tf.train.exponential_decay(base_lr, global_step=global_step, decay_steps=lr_decay_step, decay_rate=args.lr_decay) # optimizer update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): #tf.train.RMSPropOptimizer(learning_rate=self.lr, decay=0.9, momentum=0.9) train_op = tf.train.AdamOptimizer( learning_rate=lr, beta1=args.beta1).minimize(total_loss) # summary tf.summary.scalar('total_loss', total_loss) tf.summary.scalar('accuracy', acc) tf.summary.scalar('learning_rate', lr) summary_op = tf.summary.merge_all() # summary writer writer = tf.summary.FileWriter(args.logs_dir, sess.graph) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) step = 0 # saver for save/restore model saver = tf.train.Saver(max_to_keep=15) # load pretrained model if not args.renew: print('[*] Try to load trained model...') load(sess, saver, args.checkpoint_dir) max_steps = int(args.num_samples / args.batch_size) _epoch = 1 print(time.strftime("%X"), 'START TRAINING...') try: while not coord.should_stop(): print('epoch:%d/%d' % (_epoch, args.epoch)) for _step in range(step + 1, step + max_steps + 1): #start_time = time.time() gl_step = (_epoch - 1) * max_steps + _step feed_dict = {global_step: gl_step} # train _, _lr, _summ, _loss, _acc = sess.run( [train_op, lr, summary_op, total_loss, acc], feed_dict=feed_dict) # print logs 、write summary 、save model if _step % 20 == 0: writer.add_summary(_summ, _step) print( time.strftime("%X"), 'global_step:{0}, lr:{1:.8f}, acc:{2:.6f}, loss:{3:.6f}' .format(gl_step, _lr, _acc, _loss)) save_path = saver.save(sess, os.path.join(args.checkpoint_dir, args.model_name), global_step=_epoch) print('Current model saved in ' + save_path) _epoch += 1 except tf.errors.OutOfRangeError: tf.train.write_graph(sess.graph_def, args.checkpoint_dir, args.model_name + '.pb') finally: coord.request_stop() coord.join(threads) sess.close() print('FINISHED TRAINING.')
def train(input_ckpt,output_ckpt,tfrec_dir,tboard_dir,input_height,input_width, \ input_chan,batchsize,epochs,learnrate,target_acc): ''' tf.data pipelines ''' # train and test folders train_dataset = input_fn_trn(tfrec_dir, batchsize) test_dataset = input_fn_test(tfrec_dir, batchsize) ''' Call backs ''' tb_call = TensorBoard(log_dir=tboard_dir) chkpt_call = ModelCheckpoint(filepath=output_ckpt, monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=True) early_stop_call = EarlyStoponAcc(target_acc) callbacks_list = [tb_call, chkpt_call, early_stop_call] # if required, tf.set_pruning_mode must be set before defining the model if (input_ckpt != ''): tf.set_pruning_mode() ''' Define the model ''' model = mobilenetv2(input_shape=(input_height, input_width, input_chan), classes=2, alpha=1.0, incl_softmax=False) ''' Compile model Adam optimizer to change weights & biases Loss function is categorical crossentropy ''' model.compile(optimizer=tf.train.AdamOptimizer(learning_rate=learnrate), loss=SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) ''' If an input checkpoint is specified then assume we are fine-tuning a pruned model, so load the weights into the model, otherwise we are training from scratch ''' if (input_ckpt != ''): print('Loading checkpoint - fine-tuning from', input_ckpt) model.load_weights(input_ckpt) else: print('Training from scratch..') print('\n' + DIVIDER) print(' Model Summary') print(DIVIDER) print(model.summary()) print("Model Inputs: {ips}".format(ips=(model.inputs))) print("Model Outputs: {ops}".format(ops=(model.outputs))) ''' Training ''' print('\n' + DIVIDER) print(' Training model with training set..') print(DIVIDER) # make folder for saving trained model checkpoint os.makedirs(os.path.dirname(output_ckpt), exist_ok=True) # run training train_history = model.fit(train_dataset, epochs=epochs, steps_per_epoch=20000 // batchsize, validation_data=test_dataset, validation_steps=5000 // batchsize, callbacks=callbacks_list, verbose=1) ''' save just the model architecture (no weights) to a JSON file ''' with open(os.path.join(os.path.dirname(output_ckpt), 'baseline_arch.json'), 'w') as f: f.write(model.to_json()) print( "\nTensorBoard can be opened with the command: tensorboard --logdir={dir} --host localhost --port 6006" .format(dir=tboard_dir)) return
shuffle=False) cl_featurenum = 1920 # Model if opt.model == 'VGG19': net = VGG('VGG19') elif opt.model == 'Resnet18': net = ResNet18() elif opt.model == 'mobileNet_V1': net = mobilenet(num_classes=7) elif opt.model == 'mobileNet_05': net = mobilenet_05(num_classes=7) elif opt.model == 'mobileResNet_v1': net = mobileResnet(num_classes=7) elif opt.model == 'mobilenetv2': net = mobilenetv2(num_classes=7, input_size=96) elif opt.model == 'centerloss': net = mobileResnetCL(num_classes=7) pretrained_net = mobileResnet(num_classes=7) elif opt.model == 'mobilev1_CL': net = mobilenetv1CL(num_classes=7) cl_featurenum = 1024 pretrained_net = mobilenet(num_classes=7) elif opt.model == 'mobilenetv2_CL': net = mobilenetv2CL(num_classes=7, input_size=96) pretrained_net = mobilenetv2(num_classes=1000, input_size=96) cl_featurenum = 1280 elif opt.model == 'mobileDensev1_CL': net = mobileDensenetCL(num_classes=7) pretrained_net = mobilenetv1CL(num_classes=7) cl_featurenum = 512
import sys sys.path.append('../runner/') from runner import train_model, train_cifar from mobilenetv2 import mobilenetv2 from torchvision.models import resnet50 import torch import torch.nn as nn import torch.optim as optim model = mobilenetv2(10, init_weights=True) epochs = 350 opt = optim.SGD(model.parameters(), lr=0.2) sched = optim.lr_scheduler.StepLR(opt, step_size=150, gamma=0.1) # opt = optim.RMSprop(model.parameters(), lr=0.045) # opt = optim.Adam(model.parameters()) train_cifar(model, epochs, opt, verbose=True, sched=sched)
#%% import torch import torch.nn as nn from torchsummary import summary from torchvision import models from pthflops import count_ops import mobilenetv2 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # net = models.mobilenet_v2().to(device) net = mobilenetv2.mobilenetv2().to(device) net.eval() #%% summary(net, (3, 224, 224)) inp = torch.rand(1, 3, 224, 224).to(device) count_ops(net, inp)