def get_model(opt): state_dict = None if opt.pretrained and opt.network != 'resnext_wsl': state_dict = torch.utils.model_zoo.load_url(model_urls[opt.network+str(opt.layers)]) if opt.network == 'resnet': model = resnet(opt.classes, opt.layers, state_dict) elif opt.network == 'resnext': model = resnext(opt.classes, opt.layers, state_dict) elif opt.network == 'resnext_wsl': # resnext_wsl must specify the opt.battleneck_width parameter opt.network = 'resnext_wsl_32x' + str(opt.battleneck_width) +'d' model = resnext_wsl(opt.classes, opt.battleneck_width) elif opt.network == 'vgg': model = vgg_bn(opt.classes, opt.layers, state_dict) elif opt.network == 'densenet': pattern = re.compile( r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$') for key in list(state_dict.keys()): res = pattern.match(key) if res: new_key = res.group(1) + res.group(2) state_dict[new_key] = state_dict[key] del state_dict[key] model = densenet(opt.classes, opt.layers, state_dict) elif opt.network == 'inception_v3': model = inception_v3(opt.classes, opt.layers, state_dict) elif opt.network == 'dpn': model = dpn(opt.classes, opt.layers, opt.pretrained) elif opt.network == 'effnet': model = effnet(opt.classes, opt.layers, opt.pretrained) # elif opt.network == 'pnasnet_m': # model = pnasnet_m(opt.classes, opt.layers, opt.pretrained) return model
def __init__(self, config): if not isinstance(config, str): self.config = config else: assert os.path.exists(config) self.config = json.load(open(config)) assert 'name' in self.config assert 'data_path' in self.config assert 'balanced' in self.config assert 'num_samples' in self.config os.environ["CUDA_VISIBLE_DEVICES"] = self.config['gpu_ids'] self.date = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime()) # create project folder if not os.path.exists(os.path.join('./results/', self.config['name'])): os.makedirs(os.path.join('./results/', self.config['name'])) if self.config['model']['name'] == 'resnet50': self.model = resnet50( pretrained=True, num_classes=self.config['model']['num_classes']) elif self.config['model']['name'] == 'resnet101': self.model = resnet101( pretrained=True, num_classes=self.config['model']['num_classes']) elif self.config['model']['name'] == 'inception_v3': self.model = inception_v3( pretrained=True, num_classes=self.config['model']['num_classes']) elif self.config['model']['name'] == 'vgg16': self.model = vgg16(pretrained=True, num_classes=self.config['model']['num_classes']) elif self.config['model']['name'] == 'vgg19_bn': self.model = vgg19_bn( pretrained=True, num_classes=self.config['model']['num_classes']) elif self.config['model']['name'] == 'ws_dan_resnet50': self.model = ws_dan_resnet50( pretrained=True, num_classes=self.config['model']['num_classes'], num_attentions=self.config['model']['num_attentions']) self.model.cuda() if len(self.config['gpu_ids']) > 1: self.model = nn.DataParallel(self.model) #self.criterion = nn.CrossEntropyLoss() self.criterion = nn.CrossEntropyLoss(weight=torch.FloatTensor( [1.0, 3.0]).cuda()) #数据不均衡时可修改损失函数权重 self.criterion_attention = nn.MSELoss() self.optimizer = optim.SGD(self.model.parameters(), lr=self.config['model']['init_lr'], momentum=0.9, weight_decay=1e-4) # self.exp_lr_schedler = lr_scheduler.MultiStepLR(self.optimizer, milestones=self.config['model']['milestones'], gamma=0.1) # self.exp_lr_schedler = lr_scheduler.StepLR(self.optimizer, step_size=2, gamma=0.9) self.exp_lr_schedler = lr_scheduler.StepLR(self.optimizer, step_size=10, gamma=0.6)
def main(opt): if torch.cuda.is_available(): device = torch.device('cuda') torch.cuda.set_device(opt.gpu_id) else: device = torch.device('cpu') if opt.network == 'resnet': model = resnet(opt.classes, opt.layers) elif opt.network == 'resnext': model = resnext(opt.classes, opt.layers) elif opt.network == 'resnext_wsl': # resnext_wsl must specify the opt.battleneck_width parameter opt.network = 'resnext_wsl_32x' + str(opt.battleneck_width) + 'd' model = resnext_wsl(opt.classes, opt.battleneck_width) elif opt.network == 'vgg': model = vgg_bn(opt.classes, opt.layers) elif opt.network == 'densenet': model = densenet(opt.classes, opt.layers) elif opt.network == 'inception_v3': model = inception_v3(opt.classes, opt.layers) model = nn.DataParallel(model, device_ids=[7, 6]) model = model.to(device) train_data, _ = utils.read_data(os.path.join(opt.root_dir, opt.train_dir), os.path.join(opt.root_dir, opt.train_label), val_num=1) val_transforms = my_transform(False, opt.crop_size) dataset = WeatherDataset(train_data[0], train_data[1], val_transforms) loader = torch.utils.data.DataLoader(dataset, batch_size=opt.batch_size, shuffle=False, num_workers=2) model.load_state_dict( torch.load(opt.model_dir + '/' + opt.network + '-' + str(opt.layers) + '-' + str(crop_size) + '_model.ckpt')) im_labels = [] for name, label in zip(im_names, labels): im_labels.append([name, label]) header = ['filename', 'type'] utils.mkdir(opt.results_dir) result = opt.network + '-' + str( opt.layers) + '-' + str(crop_size) + '_result.csv' filename = os.path.join(opt.results_dir, result) with open(filename, 'w', encoding='utf-8') as f: f_csv = csv.writer(f) f_csv.writerow(header) f_csv.writerows(im_labels)
def train(): with tf.Graph().as_default(): # With is_training=False, we get the image without distortions iterator = input.consume_tfrecord(is_training=False, batch_size=FLAGS.batch_size) images_batch, labels_batch = iterator.get_next() # Num_classes is None for fine tuning. You need to have the proper scope. # From the original model we only need the bottlenecks. with tf.contrib.slim.arg_scope(model.inception_v3_arg_scope()): bottleneck, end_points = model.inception_v3(images_batch, num_classes=None, is_training=False, dropout_keep_prob=1) # We pass the bottleneck generated in the step before to the new classifier. logits = model.fine_tuning(bottleneck, end_points) # Get the class with the highest score predictions = tf.nn.top_k(logits, k=1) # For eval you need to restore the fine tuning model saver = tf.train.Saver(tf.global_variables('InceptionV3')) saver_ft = tf.train.Saver(tf.global_variables('fine_tuning')) init = tf.global_variables_initializer() logger = init_logger() logger.info("Eval starts...") with tf.Session() as sess: sess.run(init) saver.restore(sess, tf.train.latest_checkpoint(FLAGS.ckpt_dir)) saver_ft.restore(sess, tf.train.latest_checkpoint(FLAGS.eval_dir)) success = 0 total = 0 exec_next_step = True while exec_next_step is True: try: total += 1 predicted, image, label = sess.run( [predictions, images_batch, labels_batch]) if predicted.indices[0][0] == label[0]: success += 1 logger.info('Success rate: %.2f of %i examples', success / total * 100, total) except tf.errors.OutOfRangeError: logger.info("Eval ends...") exec_next_step = False
def load_state(self, _type='test'): if self.config['model']['name'] == 'resnet50': self.model = resnet50( pretrained=False, num_classes=self.config['model']['num_classes']) elif self.config['model']['name'] == 'resnet101': self.model = resnet101( pretrained=False, num_classes=self.config['model']['num_classes']) elif self.config['model']['name'] == 'inception_v3': self.model = inception_v3( pretrained=False, num_classes=self.config['model']['num_classes']) elif self.config['model']['name'] == 'vgg16': self.model = vgg16(pretrained=False, num_classes=self.config['model']['num_classes']) elif self.config['model']['name'] == 'vgg19_bn': self.model = vgg19_bn( pretrained=False, num_classes=self.config['model']['num_classes']) elif self.config['model']['name'] == 'ws_dan_resnet50': self.model = ws_dan_resnet50( pretrained=True, num_classes=self.config['model']['num_classes'], num_attentions=self.config['model']['num_attentions']) if _type == 'test': # checkpoints = os.path.join('./zhongshan/new_test_file_20200119', self.config['name'], 'checkpoints', # self.config['test']['checkpoint']) checkpoints = os.path.join('.', self.config['test']['checkpoint']) elif _type == 'test_batch': checkpoints = self.config['test']['checkpoint'] else: checkpoints = os.path.join('./results', self.config['name'], 'checkpoints', self.config['inference']['checkpoint']) self.model.load_state_dict(torch.load(checkpoints)['state_dict']) self.model.cuda() if len(self.config['gpu_ids']) > 1: self.model = nn.DataParallel(self.model)
seed = 1234 np.random.seed(seed) # training parameters nb_epoch = config.epoch batch_size = config.batch_size num_classes = config.num_classes """ Model """ learning_rate = 0.0001 h, w = int(3072//RESIZE), int(3900//RESIZE) input_shape = (h, w, 4) # input_shape = (224, 224, 3) model = inception_v3(in_shape=input_shape, num_classes=num_classes) # model = efficientnet(in_shape=input_shape, num_classes=num_classes) # model = resnet50(in_shape=input_shape, num_classes=num_classes) # model = densenet_121(in_shape=input_shape, num_classes=num_classes) # model = mobilenet_v2(in_shape=input_shape, num_classes=num_classes) opt = optimizers.Adam(lr=learning_rate, beta_1=0.5) # optional optimization # opt = optimizers.SGD(lr=learning_rate, momentum=0.9, nesterov=True) # opt = optimizers.adamax(lr=learning_rate) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['categorical_accuracy']) bind_model(model) if config.pause: ## test mode일 때 print('Inferring Start...') nsml.paused(scope=locals()) if config.mode == 'train': ### training mode일 때
def main(opt): if torch.cuda.is_available(): device = torch.device('cuda') torch.cuda.set_device(opt.gpu_id) else: device = torch.device('cpu') if opt.network == 'resnet': model = resnet(opt.classes, opt.layers) elif opt.network == 'resnext': model = resnext(opt.classes, opt.layers) elif opt.network == 'resnext_wsl': # resnext_wsl must specify the opt.battleneck_width parameter opt.network = 'resnext_wsl_32x' + str(opt.battleneck_width) + 'd' model = resnext_wsl(opt.classes, opt.battleneck_width) elif opt.network == 'vgg': model = vgg_bn(opt.classes, opt.layers) elif opt.network == 'densenet': model = densenet(opt.classes, opt.layers) elif opt.network == 'inception_v3': model = inception_v3(opt.classes, opt.layers) elif opt.network == 'dpn': model = dpn(opt.classes, opt.layers) elif opt.network == 'effnet': model = effnet(opt.classes, opt.layers) # elif opt.network == 'pnasnet_m': # model = pnasnet_m(opt.classes, opt.layers, opt.pretrained) # model = nn.DataParallel(model, device_ids=[4]) # model = nn.DataParallel(model, device_ids=[0, 1, 2, 3]) model = nn.DataParallel(model, device_ids=[opt.gpu_id, opt.gpu_id + 1]) # model = convert_model(model) model = model.to(device) images, names = utils.read_test_data( os.path.join(opt.root_dir, opt.test_dir)) dict_ = {} for crop_size in [opt.crop_size]: if opt.tta: transforms = test_transform(crop_size) else: transforms = my_transform(False, crop_size) dataset = TestDataset(images, names, transforms) loader = torch.utils.data.DataLoader(dataset, batch_size=opt.batch_size, shuffle=False, num_workers=4) state_dict = torch.load(opt.model_dir + '/' + opt.network + '-' + str(opt.layers) + '-' + str(crop_size) + '_model.ckpt') if opt.network == 'densenet': pattern = re.compile( r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$' ) for key in list(state_dict.keys()): res = pattern.match(key) if res: new_key = res.group(1) + res.group(2) state_dict[new_key] = state_dict[key] del state_dict[key] model.load_state_dict(state_dict) if opt.vote: if opt.tta: im_names, labels = eval_model_tta(loader, model, device=device) else: im_names, labels = eval_model(loader, model, device=device) else: if opt.tta: im_names, labels = eval_logits_tta(loader, model, device=device) else: im_names, labels = eval_logits(loader, model, device) im_labels = [] # print(im_names) for name, label in zip(im_names, labels): if name in dict_: dict_[name].append(label) else: dict_[name] = [label] header = ['filename', 'type'] utils.mkdir(opt.results_dir) result = opt.network + '-' + str(opt.layers) + '-' + str( opt.crop_size) + '_result.csv' filename = os.path.join(opt.results_dir, result) with open(filename, 'w', encoding='utf-8') as f: f_csv = csv.writer(f) f_csv.writerow(header) for key in dict_.keys(): v = np.argmax(np.sum(np.array(dict_[key]), axis=0)) + 1 # v = list(np.sum(np.array(dict_[key]), axis=0)) f_csv.writerow([key, v])
import numpy as np import tensorflow as tf from util.image_utils import image_batch from tensorflow.python.framework import graph_util RESIZE_FINAL = 227 AGE_LIST = [ '(0, 2)', '(4, 6)', '(8, 12)', '(15, 20)', '(25, 32)', '(38, 43)', '(48, 53)', '(60, 100)' ] with tf.Session() as sess: nlabels = len(AGE_LIST) from model import inception_v3 images = tf.placeholder(tf.float32, [None, 227, 227, 3], name='input') logits = inception_v3(nlabels, images, 1, False) init = tf.global_variables_initializer() saver = tf.train.Saver() saver.restore(sess, 'D:\\model\\age\\inception\\checkpoint-14999') softmax_output = tf.nn.softmax(logits, name='softmax') image = image_batch("../test1.jpg") batch_results = sess.run(softmax_output, feed_dict={images: image.eval()}) output = batch_results[0] batch_sz = batch_results.shape[0] for i in range(1, batch_sz): output = output + batch_results[i]
model = model_.ResNet_fb(n_z=args.latent_size, proj_size=None) elif args.model == 'resnet_mfcc': model = model_.ResNet_mfcc(n_z=args.latent_size, proj_size=None, ncoef=args.ncoef) elif args.model == 'resnet_lstm': model = model_.ResNet_lstm(n_z=args.latent_size, proj_size=None, ncoef=args.ncoef) elif args.model == 'resnet_stats': model = model_.ResNet_stats(n_z=args.latent_size, proj_size=None, ncoef=args.ncoef) elif args.model == 'inception_mfcc': model = model_.inception_v3(n_z=args.latent_size, proj_size=None, ncoef=args.ncoef) elif args.model == 'resnet_large': model = model_.ResNet_large_lstm(n_z=args.latent_size, proj_size=None, ncoef=args.ncoef) ckpt = torch.load(args.cp_path, map_location=lambda storage, loc: storage) model.load_state_dict(ckpt['model_state'], strict=False) model.eval() if args.cuda: model = model.cuda(device) enroll_data = None
config = args.parse_args() seed = 1234 np.random.seed(seed) def nsml_load(dir_name): model.load_weights(os.path.join(dir_name, 'model')) print('model loaded!') """ Model """ h, w = int(3072 // RESIZE), int(3900 // RESIZE) input_shape = (h, w, 4) num_classes = config.num_classes inception_model = inception_v3(input_shape, num_classes) efficient_model = inception_v3(input_shape, num_classes) # mobilenet_model = mobilenet_v2(input_shape, num_classes) # resnet_model = resnext_50(input_shape, num_classes) # densenet_model = densenet_121(input_shape, num_classes) learning_rate = 1e-4 sgd = optimizers.SGD(lr=learning_rate, momentum=0.9, nesterov=True) inception_model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['categorical_accuracy']) efficient_model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['categorical_accuracy']) # mobilenet_model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['categorical_accuracy'])
centres_type_dict[count]=0 else: centres_type_dict[count]=1 count=count+1 print(centres_type_dict,count) class_patients=[] for i in range(count): class_patients.append(centres_type_dict[i]) class_centres=[0,1] print(device,centres_dict,len(centres_dict.keys()),centres_type_dict,class_patients) model=m.inception_v3(pretrained=True) model.aux_logits=True num_ftrs = model.fc.in_features model.fc = nn.Linear(num_ftrs, 2) model.AuxLogits=m.InceptionAux(768,2) model = model.to(device) print(class_centres) center_loss_1 = CenterLoss(patients_class=class_patients, num_classes=len(centres_type_dict.keys()), feat_dim=2048, device=device ,use_gpu=True) center_loss_2 = CenterLoss(patients_class=class_centres, num_classes=2, feat_dim=2048, device=device ,use_gpu=True) focal_cross_loss=FocalLoss(class_num=2,device=device)
num_workers=2) test_data = customData( img_path='/media/data/seg_dataset/corrosion/JPEGImages/', txt_path='/media/data/seg_dataset/corrosion/label_cls_test.txt', data_transforms=transform_test) testloader = DataLoader(test_data, batch_size=2, shuffle=False, num_workers=2) # class classes = {0: 'background', 1: 'corrosion'} if TRAIN == True: # fine tuning if INCEPTION: if PRETRAINED: net = inception_v3(pretrained=PRETRAINED) for param in net.parameters(): param.requires_grad = False net.fc = torch.nn.Linear(2048, 2) else: net = inception_v3(pretrained=PRETRAINED, num_classes=len(classes)) final_conv = 'Mixed_7c' net.cuda() # print(summary(net, (3, 224, 224))) # input('s') # load checkpoint # if RESUME != 0: # print("===> Resuming from checkpoint.") # assert os.path.isfile('checkpoint/'+ str(RESUME) + '.pt'), 'Error: no checkpoint found!' # net.load_state_dict(torch.load('checkpoint/' + str(RESUME) + '.pt')) # retrain
def train(): with tf.Graph().as_default() as g: global_step = tf.train.get_or_create_global_step() # Get the iterator from the TFRecord files. iterator = input.consume_tfrecord() images_batch, labels_batch = iterator.get_next() # Num_classes is None for fine tuning. You need to have the proper scope. # From the original model we only need the bottlenecks. with tf.contrib.slim.arg_scope(model.inception_v3_arg_scope()): bottleneck, end_points = model.inception_v3(images_batch, num_classes=None, is_training=False) # We pass the bottleneck generated in the step before to the new classifier. logits = model.fine_tuning(bottleneck, end_points) # We compute the loss between the predictions and the labels loss = model.loss(logits, labels_batch) # We use ADAM as a optimizer. You could use whichever you want, like Gradient Descent. # It's important to indicate that we only want to retrain the 'fine_tuning' variables. optimizer = tf.train.AdamOptimizer(0.005) train_op = optimizer.minimize( loss, global_step=global_step, var_list=tf.global_variables('fine_tuning')) # We create two savers. The first one for the InceptionV3 variables and the second one for the variables of # the new classifier. saver = tf.train.Saver(tf.global_variables('InceptionV3')) saver_ft = tf.train.Saver(tf.global_variables('fine_tuning')) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) # Restore the checkpoints of the InceptionV3 model. saver.restore(sess, tf.train.latest_checkpoint(FLAGS.ckpt_dir)) # This will let you see the images in tensorboard tf.summary.image(tensor=images_batch, name="Image") # Tensorborad options train_writer = tf.summary.FileWriter(FLAGS.log_dir, g) logger = init_logger() logger.info("Training starts...") # Training loop. Set the max number of steps. for i in range(0, FLAGS.max_steps): # We compute the image and label batch sess.run([images_batch, labels_batch]) # Merge all summary variables for Tensorborad merge = tf.summary.merge_all() # We do the training and compute the loss and the summaries _, loss_val, summary = sess.run([train_op, loss, merge]) if i % 10 is 0: logger.info('Time: %s Loss: %f Step: %i', datetime.now(), loss_val, i) # Write the summaries in the log file train_writer.add_summary(summary, i) # We save the progress every 500 steps if i % 500 is 0 and i is not 0: saver_ft.save(sess, FLAGS.save_dir, global_step=global_step) logger.info("***** Saving model in: %s *****", FLAGS.save_dir) logger.info("Training ends...") saver_ft.save(sess, FLAGS.save_dir, global_step=global_step) logger.info("***** Saving model in: %s *****", FLAGS.save_dir)
import numpy as np from model import alexnet2, sterringNet, inception_v3 from numpy.random import shuffle WIDTH = 320 HEIGHT = 200 LR = 1e-3 EPOCHS = 16 LOAD_MODEL = True FILE_ID_END = 1 FILE_ID_START = 0 MODEL_NAME = 'car-front-steering-regression-inceptionv3.model' model = inception_v3() #model = alexnet2(WIDTH, HEIGHT, LR, output=2) # MODEL_NAME = 'sam-{}-{}-{}-epochs.model'.format(LR, 'alexnet2',EPOCHS) # model = alexnet2(WIDTH,HEIGHT, LR,output=9) # PRE_NAME = 'model_alexnet-2390' if LOAD_MODEL: model.load(MODEL_NAME) print('We have loaded a previous model!!!!') def prepare_data(train_data): train = train_data[:-100] test = train_data[-100:] X = np.array([i[0] for i in train]).reshape(-1, WIDTH, HEIGHT, 3) Y = [i[1] for i in train] #steering Y = [[0.5 - i[0], i[0] + 0.5] for i in Y]