def main(): # Parse the JSON arguments config_args = parse_args() # Create the experiment directories _, config_args.summary_dir, config_args.checkpoint_dir = create_experiment_dirs( config_args.experiment_dir) model = MobileNetV2(config_args) if config_args.cuda: model.cuda() cudnn.enabled = True cudnn.benchmark = True print("Loading Data...") data = BenchPressData(config_args) print("Data loaded successfully\n") trainer = Train(model, data.trainloader, data.testloader, config_args) if config_args.to_train: try: print("Training...") trainer.train() print("Training Finished\n") except KeyboardInterrupt: pass if config_args.to_test: print("Testing...") trainer.test(data.testloader) print("Testing Finished\n")
def test(): model = MobileNetV2() pthfile = '/home/pateo/JUL/CODE/deep-learning-for-image-processing-master/pytorch_classification/Test6_mobilenet/MobileNetV2.pth' loaded_model = torch.load(pthfile, map_location='cpu') # try: # loaded_model.eval() # except AttributeError as error: # print(error) # model.load_state_dict(loaded_model['state_dict']) # model = model.to(device) # data type nchw dummy_input1 = torch.randn(1, 3, 64, 64) # dummy_input2 = torch.randn(1, 3, 64, 64) # dummy_input3 = torch.randn(1, 3, 64, 64) input_names = ["actual_input_1"] output_names = ["output1"] # torch.onnx.export(model, (dummy_input1, dummy_input2, dummy_input3), "C3AE.onnx", verbose=True, input_names=input_names, output_names=output_names) torch.onnx.export(model, dummy_input1, "MobileNetV2.onnx", verbose=True, input_names=input_names, output_names=output_names)
def __init__(self): self.model_path = '../saved_models/mobilenet.pt' self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') self.model = MobileNetV2() self.model.load_state_dict(torch.load(self.model_path), strict=False) self.model = self.model.to(self.device) self.model.eval() self.image_transform = transforms.Compose([transforms.Resize((256, 256)), transforms.ToTensor(), transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
def __init__(self, architecture, task_info): super(SingleTaskModel, self).__init__(architecture, task_info) self.device = torch.device( 'cuda' if torch.cuda.is_available() else 'cpu') self.model = MobileNetV2(architecture=architecture, in_channels=task_info.num_channels, num_classes=[task_info.num_classes]) self.model = nn.DataParallel(self.model).to(self.device)
def main(): # parse arguments args=parse_args() if args.cpu: os.environ["CUDA_VISIBLE_DEVICES"] = "" sess = tf.Session() if args.is_train: datas=[] # read train images and labels with open(args.dataset_txt,'r') as file: for line in file: f_name, label=line.strip().split() path=os.path.join(args.dataset_dir, f_name) datas.append([path, int(label)]) # check dirs if not os.path.exists(args.checkpoint_dir): os.makedirs(args.checkpoint_dir) if not os.path.exists(args.logs_dir): os.makedirs(args.logs_dir) model=MobileNetV2(sess=sess, dataset=np.array(datas), epoch=args.epoch, batch_size=args.batch_size, image_height=args.image_height, image_width=args.image_width, n_classes=args.n_classes, is_train=args.is_train, learning_rate=args.learning_rate, lr_decay=args.lr_decay,beta1=args.beta1, chkpt_dir=args.checkpoint_dir, logs_dir=args.logs_dir, model_name=args.model_name, rand_crop=args.rand_crop) model._build_train_graph() model._train() else: # restore model saver = tf.train.import_meta_graph(os.path.join(args.checkpoint_dir,args.model_name+'.meta')) saver.restore(sess, tf.train.latest_checkpoint(args.checkpoint_dir)) # get input and output tensors from graph graph = tf.get_default_graph() input_x = graph.get_tensor_by_name("input:0") input_y = graph.get_tensor_by_name("label:0") prob = graph.get_tensor_by_name("mobilenetv2/prob:0") # prepare eval/test data and label img=imread('data/tmp/art01.jpg') img = imresize(img, (args.image_height, args.image_width)) label=1 feed_dict={input_x:[img],input_y:[label]} # use [], because we need 4-D tensor start=time.time() res=sess.run(prob, feed_dict=feed_dict)[0] # index 0 for batch_size print('prob: {}'.format(res)) print('time: {}'.format(time.time()-start))
def main(): im_height = 224 im_width = 224 num_classes = 5 # load image img_path = "../tulip.jpg" assert os.path.exists(img_path), "file: '{}' dose not exist.".format( img_path) img = Image.open(img_path) # resize image to 224x224 img = img.resize((im_width, im_height)) plt.imshow(img) # scaling pixel value to (-1,1) img = np.array(img).astype(np.float32) img = ((img / 255.) - 0.5) * 2.0 # Add the image to a batch where it's the only member. img = (np.expand_dims(img, 0)) # read class_indict json_path = './class_indices.json' assert os.path.exists(json_path), "file: '{}' dose not exist.".format( json_path) json_file = open(json_path, "r") class_indict = json.load(json_file) # create model feature = MobileNetV2(include_top=False) model = tf.keras.Sequential([ feature, tf.keras.layers.GlobalAvgPool2D(), tf.keras.layers.Dropout(rate=0.5), tf.keras.layers.Dense(num_classes), tf.keras.layers.Softmax() ]) weights_path = './save_weights/resMobileNetV2.ckpt' assert len(glob.glob(weights_path + "*")), "cannot find {}".format(weights_path) model.load_weights(weights_path) result = np.squeeze(model.predict(img)) predict_class = np.argmax(result) print_res = "class: {} prob: {:.3}".format( class_indict[str(predict_class)], result[predict_class]) plt.title(print_res) print(print_res) plt.show()
def __init__(self, policy, nact, nenvs, nsteps, wave_num, k, ent_coef=0.01, vf_coef=0.5, base_lr=7e-4, weight_decay=1e-5, momentum=0.9, total_timesteps=int(80e6), lrschedule='linear'): """ A2C的模型,除了超参的设置以外,主要是model(在openai的A2C中是包括step_model和train_model),openai的实现是基于tensorflow的, step_model和train_model 共享基础变量。而在Pytorch中直接以model实现,具体的功能区分,在model中实现。 :param policy: 神经网络模型的类,用以生成model :param nact: number of action space (Discrete) :param nenvs: environments number。表示同时进行的游戏进程数 :param nsteps: 进行一次训练,所需要游戏进行的步数 :param wave_num: 链路波长数 :param k: 表示ksp算法中k的取值,关系到action space :param ent_coef: entropy coefficient 熵系数,意义不明 :param vf_coef: value function coefficient 价值函数系数,意义不明 :param base_lr: 初始学习率 :param momentum: 动量,SGD优化器的参数 :param total_timesteps: 一共要进行的步骤 :param lrschedule: 学习率调整方式 """ self.base_lr = base_lr self.total_timesteps = total_timesteps nbatch = nenvs * nsteps # 一次训练的batch_size大小 if policy == MobileNetV2: model = policy(in_channels=wave_num, num_classes=k * wave_num + 1, t=6) else: raise NotImplementedError model = MobileNetV2() optimizer = torch.optim.SGD(params=model.parameters(), lr=base_lr, momentum=momentum, weight_decay=weight_decay) torch.optim.lr_scheduler.StepLR()
def main(): device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") data_transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) # load image img_path = "../tulip.jpg" assert os.path.exists(img_path), "file: '{}' dose not exist.".format( img_path) img = Image.open(img_path) plt.imshow(img) # [N, C, H, W] img = data_transform(img) # expand batch dimension img = torch.unsqueeze(img, dim=0) # read class_indict json_path = './class_indices.json' assert os.path.exists(json_path), "file: '{}' dose not exist.".format( json_path) json_file = open(json_path, "r") class_indict = json.load(json_file) # create model model = MobileNetV2(num_classes=5).to(device) # load model weights model_weight_path = "./MobileNetV2.pth" model.load_state_dict(torch.load(model_weight_path, map_location=device)) model.eval() with torch.no_grad(): # predict class output = torch.squeeze(model(img.to(device))).cpu() predict = torch.softmax(output, dim=0) predict_cla = torch.argmax(predict).numpy() print_res = "class: {} prob: {:.3}".format( class_indict[str(predict_cla)], predict[predict_cla].numpy()) plt.title(print_res) print(print_res) plt.show()
def main(): global actor_critic, directory, weight num_cls = args.wave_num * args.k + 1 # 所有的路由和波长选择组合,加上啥都不选 if args.append_route.startswith("True"): channel_num = args.wave_num+args.k else: channel_num = args.wave_num # 解析weight if args.weight.startswith('None'): weight = None else: weight = args.weight # CNN学习模式下,osb的shape应该是CHW assert args.mode.startswith('learning') # 模型初始化 if args.cnn.startswith('mobilenetv2'): actor_critic = MobileNetV2(in_channels=channel_num, num_classes=num_cls, t=6) elif args.cnn.startswith('simplenet'): actor_critic = SimpleNet(in_channels=channel_num, num_classes=num_cls) elif args.cnn.startswith('simplestnet'): actor_critic = SimplestNet(in_channels=channel_num, num_classes=num_cls) elif args.cnn.startswith('alexnet'): actor_critic = AlexNet(in_channels=channel_num, num_classes=num_cls) elif args.cnn.startswith('squeezenet'): actor_critic = SqueezeNet(in_channels=channel_num, num_classes=num_cls, version=1.0) else: raise NotImplementedError times = 1 # 重复次数 prefix = "trained_models" directory = os.path.join(prefix, 'a2c', args.cnn, args.step_over) if args.comp.startswith("states"): all_states_comp() elif args.comp.startswith("random"): random_comp(times=times) elif args.comp.startswith("None"): raise ValueError("Wrong call for this script") else: raise NotImplementedError
def main(): """ Script entrypoint """ t_start = datetime.now() header = ["Start Time", "End Time", "Duration (s)"] row = [t_start.strftime(DEFAULT_DATE_TIME_FORMAT)] dnn = MobileNetV2() # show class indices print('****************') for cls, idx in dnn.train_batches.class_indices.items(): print('Class #{} = {}'.format(idx, cls)) print('****************') print(dnn.model.summary()) dnn.train(t_start, epochs=dnn.num_epochs, batch_size=dnn.batch_size, training=dnn.train_batches, validation=dnn.valid_batches) # save trained weights dnn.model.save(dnn.file_weights + 'old') dnn.model.save_weights(dnn.file_weights) with open(dnn.file_architecture, 'w') as f: f.write(dnn.model.to_json()) t_end = datetime.now() difference_in_seconds = get_difference_in_seconds(t_start, t_end) row.append(t_end.strftime(DEFAULT_DATE_TIME_FORMAT)) row.append(str(difference_in_seconds)) append_row_to_csv(complete_run_timing_file, header) append_row_to_csv(complete_run_timing_file, row)
def main(): gpus = tf.config.experimental.list_physical_devices("GPU") if gpus: try: for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) except RuntimeError as e: print(e) exit(-1) data_root = os.path.abspath(os.path.join(os.getcwd(), "../..")) # get data root path image_path = os.path.join(data_root, "data_set", "flower_data") # flower data set path train_dir = os.path.join(image_path, "train") validation_dir = os.path.join(image_path, "val") assert os.path.exists(train_dir), "cannot find {}".format(train_dir) assert os.path.exists(validation_dir), "cannot find {}".format( validation_dir) # create direction for saving weights if not os.path.exists("save_weights"): os.makedirs("save_weights") im_height = 224 im_width = 224 batch_size = 32 epochs = 30 # class dict data_class = [ cla for cla in os.listdir(train_dir) if os.path.isdir(os.path.join(train_dir, cla)) ] class_num = len(data_class) class_dict = dict((value, index) for index, value in enumerate(data_class)) # reverse value and key of dict inverse_dict = dict((val, key) for key, val in class_dict.items()) # write dict into json file json_str = json.dumps(inverse_dict, indent=4) with open('class_indices.json', 'w') as json_file: json_file.write(json_str) # load train images list train_image_list = glob.glob(train_dir + "/*/*.jpg") random.shuffle(train_image_list) train_num = len(train_image_list) assert train_num > 0, "cannot find any .jpg file in {}".format(train_dir) train_label_list = [ class_dict[path.split(os.path.sep)[-2]] for path in train_image_list ] # load validation images list val_image_list = glob.glob(validation_dir + "/*/*.jpg") random.shuffle(val_image_list) val_num = len(val_image_list) assert val_num > 0, "cannot find any .jpg file in {}".format( validation_dir) val_label_list = [ class_dict[path.split(os.path.sep)[-2]] for path in val_image_list ] print("using {} images for training, {} images for validation.".format( train_num, val_num)) def process_train_img(img_path, label): label = tf.one_hot(label, depth=class_num) image = tf.io.read_file(img_path) image = tf.image.decode_jpeg(image) image = tf.image.convert_image_dtype(image, tf.float32) image = tf.image.resize(image, [im_height, im_width]) image = tf.image.random_flip_left_right(image) # image = (image - 0.5) / 0.5 image = (image - 0.5) * 2.0 return image, label def process_val_img(img_path, label): label = tf.one_hot(label, depth=class_num) image = tf.io.read_file(img_path) image = tf.image.decode_jpeg(image) image = tf.image.convert_image_dtype(image, tf.float32) image = tf.image.resize(image, [im_height, im_width]) # image = (image - 0.5) / 0.5 image = (image - 0.5) * 2.0 return image, label AUTOTUNE = tf.data.experimental.AUTOTUNE # load train dataset train_dataset = tf.data.Dataset.from_tensor_slices( (train_image_list, train_label_list)) train_dataset = train_dataset.shuffle(buffer_size=train_num)\ .map(process_train_img, num_parallel_calls=AUTOTUNE)\ .repeat().batch(batch_size).prefetch(AUTOTUNE) # load train dataset val_dataset = tf.data.Dataset.from_tensor_slices( (val_image_list, val_label_list)) val_dataset = val_dataset.map(process_val_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)\ .repeat().batch(batch_size) # 实例化模型 model = MobileNetV2(num_classes=5) pre_weights_path = './pretrain_weights.ckpt' assert len(glob.glob(pre_weights_path + "*")), "cannot find {}".format(pre_weights_path) model.load_weights(pre_weights_path) for layer_t in model.layers[:-1]: layer_t.trainable = False model.summary() # using keras low level api for training loss_object = tf.keras.losses.CategoricalCrossentropy(from_logits=True) optimizer = tf.keras.optimizers.Adam(learning_rate=0.0005) train_loss = tf.keras.metrics.Mean(name='train_loss') train_accuracy = tf.keras.metrics.CategoricalAccuracy( name='train_accuracy') test_loss = tf.keras.metrics.Mean(name='test_loss') test_accuracy = tf.keras.metrics.CategoricalAccuracy(name='test_accuracy') @tf.function def train_step(images, labels): with tf.GradientTape() as tape: output = model(images, training=True) loss = loss_object(labels, output) gradients = tape.gradient(loss, model.trainable_variables) optimizer.apply_gradients(zip(gradients, model.trainable_variables)) train_loss(loss) train_accuracy(labels, output) @tf.function def test_step(images, labels): output = model(images, training=False) t_loss = loss_object(labels, output) test_loss(t_loss) test_accuracy(labels, output) best_test_loss = float('inf') train_step_num = train_num // batch_size val_step_num = val_num // batch_size for epoch in range(1, epochs + 1): train_loss.reset_states() # clear history info train_accuracy.reset_states() # clear history info test_loss.reset_states() # clear history info test_accuracy.reset_states() # clear history info t1 = time.perf_counter() for index, (images, labels) in enumerate(train_dataset): train_step(images, labels) if index + 1 == train_step_num: break print(time.perf_counter() - t1) for index, (images, labels) in enumerate(val_dataset): test_step(images, labels) if index + 1 == val_step_num: break template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy: {}' print( template.format(epoch, train_loss.result(), train_accuracy.result() * 100, test_loss.result(), test_accuracy.result() * 100)) if test_loss.result() < best_test_loss: model.save_weights("./save_weights/myMobileNet.ckpt".format(epoch), save_format='tf')
model = DenseModel() elif args.enc_model == "resnet": print("ResNet Model") from model import ResNetModel model = ResNetModel() elif args.enc_model == "vgg": print("VGG Model") from model import VGGModel model = VGGModel() elif args.enc_model == "mobilenet": print("Mobile NetV2") from model import MobileNetV2 model = MobileNetV2() if args.enc_model != "mobilenet": model = nn.DataParallel(model) model.load_state_dict(torch.load(args.model_val_path)) model = model.to(device) val_img_ids = os.listdir(args.val_img_dir) val_dataset = TestLoader(args.val_img_dir, val_img_ids) vis_loader = torch.utils.data.DataLoader(val_dataset, batch_size=1, shuffle=False, num_workers=args.no_workers)
def main(): device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print("using {} device.".format(device)) data_transform = { "train": transforms.Compose([ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]), "val": transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) } data_root = os.path.abspath(os.path.join(os.getcwd(), "../..")) # get data root path image_path = os.path.join(data_root, "data_set", "flower_data") # flower data set path assert os.path.exists(image_path), "{} path does not exist.".format( image_path) train_dataset = datasets.ImageFolder(root=os.path.join( image_path, "train"), transform=data_transform["train"]) train_num = len(train_dataset) # {'daisy':0, 'dandelion':1, 'roses':2, 'sunflower':3, 'tulips':4} flower_list = train_dataset.class_to_idx cla_dict = dict((val, key) for key, val in flower_list.items()) # write dict into json file json_str = json.dumps(cla_dict, indent=4) with open('class_indices.json', 'w') as json_file: json_file.write(json_str) batch_size = 16 nw = min([os.cpu_count(), batch_size if batch_size > 1 else 0, 8]) # number of workers print('Using {} dataloader workers every process'.format(nw)) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=nw) validate_dataset = datasets.ImageFolder(root=os.path.join( image_path, "val"), transform=data_transform["val"]) val_num = len(validate_dataset) validate_loader = torch.utils.data.DataLoader(validate_dataset, batch_size=batch_size, shuffle=False, num_workers=nw) print("using {} images for training, {} images fot validation.".format( train_num, val_num)) net = MobileNetV2(num_classes=5) # load pretrain weights # download url: https://download.pytorch.org/models/mobilenet_v2-b0353104.pth model_weight_path = "./mobilenet_v2.pth" assert os.path.exists(model_weight_path), "file {} dose not exist.".format( model_weight_path) pre_weights = torch.load(model_weight_path) # delete classifier weights pre_dict = {k: v for k, v in pre_weights.items() if "classifier" not in k} missing_keys, unexpected_keys = net.load_state_dict(pre_dict, strict=False) # freeze features weights for param in net.features.parameters(): param.requires_grad = False net.to(device) loss_function = nn.CrossEntropyLoss() optimizer = optim.Adam(net.parameters(), lr=0.0001) epochs = 5 best_acc = 0.0 save_path = './MobileNetV2.pth' train_steps = len(train_loader) for epoch in range(epochs): # train net.train() running_loss = 0.0 train_bar = tqdm(train_loader) for step, data in enumerate(train_bar): images, labels = data optimizer.zero_grad() logits = net(images.to(device)) loss = loss_function(logits, labels.to(device)) loss.backward() optimizer.step() # print statistics running_loss += loss.item() train_bar.desc = "train epoch[{}/{}] loss:{:.3f}".format( epoch + 1, epochs, loss) # validate net.eval() acc = 0.0 # accumulate accurate number / epoch with torch.no_grad(): val_bar = tqdm(validate_loader, colour='green') for val_data in val_bar: val_images, val_labels = val_data outputs = net(val_images.to(device)) # loss = loss_function(outputs, test_labels) predict_y = torch.max(outputs, dim=1)[1] acc += torch.eq(predict_y, val_labels.to(device)).sum().item() val_bar.desc = "valid epoch[{}/{}]".format(epoch + 1, epochs) val_accurate = acc / val_num print('[epoch %d] train_loss: %.3f test_accuracy: %.3f' % (epoch + 1, running_loss / train_steps, val_accurate)) if val_accurate > best_acc: best_acc = val_accurate torch.save(net.state_dict(), save_path) print('Finished Training')
def main(): device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print(device) data_transform = { "train": transforms.Compose([ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]), "val": transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) } data_root = os.path.abspath(os.path.join(os.getcwd(), "../..")) # get data root path image_path = data_root + "/data/flower_data/" # flower data set path train_dataset = datasets.ImageFolder(root=image_path + "train", transform=data_transform["train"]) train_num = len(train_dataset) # {'daisy':0, 'dandelion':1, 'roses':2, 'sunflower':3, 'tulips':4} flower_list = train_dataset.class_to_idx cla_dict = dict((val, key) for key, val in flower_list.items()) # write dict into json file json_str = json.dumps(cla_dict, indent=4) with open('class_indices.json', 'w') as json_file: json_file.write(json_str) batch_size = 16 train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=0) validate_dataset = datasets.ImageFolder(root=image_path + "val", transform=data_transform["val"]) val_num = len(validate_dataset) validate_loader = torch.utils.data.DataLoader(validate_dataset, batch_size=batch_size, shuffle=False, num_workers=0) net = MobileNetV2(num_classes=5) # load pretrain weights model_weight_path = "./mobilenet_v2.pth" pre_weights = torch.load(model_weight_path) # delete classifier weights pre_dict = {k: v for k, v in pre_weights.items() if "classifier" not in k} missing_keys, unexpected_keys = net.load_state_dict(pre_dict, strict=False) # freeze features weights for param in net.features.parameters(): param.requires_grad = False net.to(device) loss_function = nn.CrossEntropyLoss() optimizer = optim.Adam(net.parameters(), lr=0.0001) best_acc = 0.0 save_path = './MobileNetV2.pth' for epoch in range(5): # train net.train() running_loss = 0.0 for step, data in enumerate(train_loader, start=0): images, labels = data optimizer.zero_grad() logits = net(images.to(device)) loss = loss_function(logits, labels.to(device)) loss.backward() optimizer.step() # print statistics running_loss += loss.item() # print train process rate = (step + 1) / len(train_loader) a = "*" * int(rate * 50) b = "." * int((1 - rate) * 50) print("\rtrain loss: {:^3.0f}%[{}->{}]{:.4f}".format( int(rate * 100), a, b, loss), end="") print() # validate net.eval() acc = 0.0 # accumulate accurate number / epoch with torch.no_grad(): for val_data in validate_loader: val_images, val_labels = val_data outputs = net(val_images.to( device)) # eval model only have last output layer # loss = loss_function(outputs, test_labels) predict_y = torch.max(outputs, dim=1)[1] acc += (predict_y == val_labels.to(device)).sum().item() val_accurate = acc / val_num if val_accurate > best_acc: best_acc = val_accurate torch.save(net.state_dict(), save_path) print('[epoch %d] train_loss: %.3f test_accuracy: %.3f' % (epoch + 1, running_loss / step, val_accurate)) print('Finished Training')
from flask_cors import CORS from model import MobileNetV2 app = Flask(__name__) CORS(app) # 解决跨域问题 weights_path = "./MobileNetV2(flower).pth" class_json_path = "./class_indices.json" assert os.path.exists(weights_path), "weights path does not exist..." assert os.path.exists(class_json_path), "class json path does not exist..." # select device device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print(device) # create model model = MobileNetV2(num_classes=5).to(device) # load model weights model.load_state_dict(torch.load(weights_path, map_location=device)) model.eval() # load class info json_file = open(class_json_path, 'rb') class_indict = json.load(json_file) def transform_image(image_bytes): my_transforms = transforms.Compose([ transforms.Resize(255), transforms.CenterCrop(224), transforms.ToTensor(),
def main(): data_root = os.path.abspath(os.path.join(os.getcwd(), "../..")) # get data root path image_path = data_root + "/data_set/flower_data/" # flower data set path train_dir = image_path + "train" validation_dir = image_path + "val" im_height = 224 im_width = 224 batch_size = 16 epochs = 20 def pre_function(img): # img = im.open('test.jpg') # img = np.array(img).astype(np.float32) img = img / 255. img = (img - 0.5) * 2.0 return img # data generator with data augmentation train_image_generator = ImageDataGenerator( horizontal_flip=True, preprocessing_function=pre_function) validation_image_generator = ImageDataGenerator( preprocessing_function=pre_function) train_data_gen = train_image_generator.flow_from_directory( directory=train_dir, batch_size=batch_size, shuffle=True, target_size=(im_height, im_width), class_mode='categorical') total_train = train_data_gen.n # get class dict class_indices = train_data_gen.class_indices # transform value and key of dict inverse_dict = dict((val, key) for key, val in class_indices.items()) # write dict into json file json_str = json.dumps(inverse_dict, indent=4) with open('class_indices.json', 'w') as json_file: json_file.write(json_str) val_data_gen = validation_image_generator.flow_from_directory( directory=validation_dir, batch_size=batch_size, shuffle=False, target_size=(im_height, im_width), class_mode='categorical') # img, _ = next(train_data_gen) total_val = val_data_gen.n model = MobileNetV2(num_classes=5) # feature.build((None, 224, 224, 3)) # when using subclass model model.load_weights('pretrain_weights.ckpt') for layer_t in model.layers[:-1]: layer_t.trainable = False model.summary() # using keras low level api for training loss_object = tf.keras.losses.CategoricalCrossentropy( from_logits=True) # not use softmax activition optimizer = tf.keras.optimizers.Adam(learning_rate=0.001) train_loss = tf.keras.metrics.Mean(name='train_loss') train_accuracy = tf.keras.metrics.CategoricalAccuracy( name='train_accuracy') test_loss = tf.keras.metrics.Mean(name='test_loss') test_accuracy = tf.keras.metrics.CategoricalAccuracy(name='test_accuracy') @tf.function def train_step(images, labels): with tf.GradientTape() as tape: output = model(images, training=True) loss = loss_object(labels, output) gradients = tape.gradient(loss, model.trainable_variables) optimizer.apply_gradients(zip(gradients, model.trainable_variables)) train_loss(loss) train_accuracy(labels, output) @tf.function def test_step(images, labels): output = model(images, training=False) t_loss = loss_object(labels, output) test_loss(t_loss) test_accuracy(labels, output) best_test_loss = float('inf') for epoch in range(1, epochs + 1): train_loss.reset_states() # clear history info train_accuracy.reset_states() # clear history info test_loss.reset_states() # clear history info test_accuracy.reset_states() # clear history info # train for step in range(total_train // batch_size): images, labels = next(train_data_gen) train_step(images, labels) # print train process rate = (step + 1) / (total_train // batch_size) a = "*" * int(rate * 50) b = "." * int((1 - rate) * 50) acc = train_accuracy.result().numpy() print("\r[{}]train acc: {:^3.0f}%[{}->{}]{:.4f}".format( epoch, int(rate * 100), a, b, acc), end="") print() # validate for step in range(total_val // batch_size): test_images, test_labels = next(val_data_gen) test_step(test_images, test_labels) template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy: {}' print( template.format(epoch, train_loss.result(), train_accuracy.result() * 100, test_loss.result(), test_accuracy.result() * 100)) if test_loss.result() < best_test_loss: best_test_loss = test_loss.result() model.save_weights("./save_weights/resMobileNetV2.ckpt", save_format="tf")
def main(): # parse arguments args = parse_args() if args.cpu: os.environ["CUDA_VISIBLE_DEVICES"] = "" sess = tf.Session() if args.is_train: # read tfrecord files glob_pattern = os.path.join(args.dataset_dir, '*.tfrecord') tfrecords_list = glob.glob(glob_pattern) # check dirs if not os.path.exists(args.checkpoint_dir): os.makedirs(args.checkpoint_dir) if not os.path.exists(args.logs_dir): os.makedirs(args.logs_dir) model = MobileNetV2(sess=sess, tf_files=tfrecords_list, num_sampes=args.num_samples, epoch=args.epoch, batch_size=args.batch_size, image_height=args.image_height, image_width=args.image_width, n_classes=args.n_classes, is_train=args.is_train, learning_rate=args.learning_rate, lr_decay=args.lr_decay, beta1=args.beta1, chkpt_dir=args.checkpoint_dir, logs_dir=args.logs_dir, model_name=args.model_name, rand_crop=args.rand_crop) model._build_train_graph() model._train() else: model = MobileNetV2(sess=sess, tf_files='', num_sampes=args.num_samples, epoch=args.epoch, batch_size=args.batch_size, image_height=args.image_height, image_width=args.image_width, n_classes=args.n_classes, is_train=args.is_train, learning_rate=args.learning_rate, lr_decay=args.lr_decay, beta1=args.beta1, chkpt_dir=args.checkpoint_dir, logs_dir=args.logs_dir, model_name=args.model_name, rand_crop=args.rand_crop) model._build_test_graph() saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(args.checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: ckpt_name = os.path.basename(ckpt.model_checkpoint_path) saver.restore(sess, os.path.join(args.checkpoint_dir, ckpt_name)) print("[*] Success to read {}".format(ckpt_name)) else: print("[*] Failed to find a checkpoint") return # get input and output tensors from graph graph = tf.get_default_graph() input_x = graph.get_tensor_by_name("input:0") input_y = graph.get_tensor_by_name("label:0") prob = graph.get_tensor_by_name("mobilenetv2/prob:0") # prepare eval/test data and label img = imread('data/test/t_1_0.jpeg') img = imresize(img, (args.image_height, args.image_width)) img = preprocess(img).astype(np.float32) print(img.dtype) label = 1 feed_dict = { input_x: [img], input_y: [label] } # use [], because we need 4-D tensor start = time.time() res = sess.run(prob, feed_dict=feed_dict)[0] # index 0 for batch_size print('prob: {}, class: {}'.format(res, np.argmax(res))) print('time: {}'.format(time.time() - start)) # close session sess.close()
def main(): data_root = os.path.abspath(os.path.join(os.getcwd(), "../..")) # get data root path image_path = os.path.join(data_root, "data_set", "flower_data") # flower data set path train_dir = os.path.join(image_path, "train") validation_dir = os.path.join(image_path, "val") assert os.path.exists(train_dir), "cannot find {}".format(train_dir) assert os.path.exists(validation_dir), "cannot find {}".format(validation_dir) im_height = 224 im_width = 224 batch_size = 16 epochs = 20 num_classes = 5 def pre_function(img): # img = im.open('test.jpg') # img = np.array(img).astype(np.float32) img = img / 255. img = (img - 0.5) * 2.0 return img # data generator with data augmentation train_image_generator = ImageDataGenerator(horizontal_flip=True, preprocessing_function=pre_function) validation_image_generator = ImageDataGenerator(preprocessing_function=pre_function) train_data_gen = train_image_generator.flow_from_directory(directory=train_dir, batch_size=batch_size, shuffle=True, target_size=(im_height, im_width), class_mode='categorical') total_train = train_data_gen.n # get class dict class_indices = train_data_gen.class_indices # transform value and key of dict inverse_dict = dict((val, key) for key, val in class_indices.items()) # write dict into json file json_str = json.dumps(inverse_dict, indent=4) with open('class_indices.json', 'w') as json_file: json_file.write(json_str) val_data_gen = validation_image_generator.flow_from_directory(directory=validation_dir, batch_size=batch_size, shuffle=False, target_size=(im_height, im_width), class_mode='categorical') # img, _ = next(train_data_gen) total_val = val_data_gen.n print("using {} images for training, {} images for validation.".format(total_train, total_val)) # create model except fc layer feature = MobileNetV2(include_top=False) # download weights 链接: https://pan.baidu.com/s/1YgFoIKHqooMrTQg_IqI2hA 密码: 2qht pre_weights_path = './pretrain_weights.ckpt' assert len(glob.glob(pre_weights_path+"*")), "cannot find {}".format(pre_weights_path) feature.load_weights(pre_weights_path) feature.trainable = False feature.summary() # add last fc layer model = tf.keras.Sequential([feature, tf.keras.layers.GlobalAvgPool2D(), tf.keras.layers.Dropout(rate=0.5), tf.keras.layers.Dense(num_classes), tf.keras.layers.Softmax()]) model.summary() # using keras low level api for training loss_object = tf.keras.losses.CategoricalCrossentropy(from_logits=False) optimizer = tf.keras.optimizers.Adam(learning_rate=0.001) train_loss = tf.keras.metrics.Mean(name='train_loss') train_accuracy = tf.keras.metrics.CategoricalAccuracy(name='train_accuracy') val_loss = tf.keras.metrics.Mean(name='val_loss') val_accuracy = tf.keras.metrics.CategoricalAccuracy(name='val_accuracy') @tf.function def train_step(images, labels): with tf.GradientTape() as tape: output = model(images, training=True) loss = loss_object(labels, output) gradients = tape.gradient(loss, model.trainable_variables) optimizer.apply_gradients(zip(gradients, model.trainable_variables)) train_loss(loss) train_accuracy(labels, output) @tf.function def val_step(images, labels): output = model(images, training=False) loss = loss_object(labels, output) val_loss(loss) val_accuracy(labels, output) best_test_loss = float('inf') for epoch in range(epochs): train_loss.reset_states() # clear history info train_accuracy.reset_states() # clear history info val_loss.reset_states() # clear history info val_accuracy.reset_states() # clear history info # train train_bar = tqdm(range(total_train // batch_size)) for step in train_bar: images, labels = next(train_data_gen) train_step(images, labels) # print train process train_bar.desc = "train epoch[{}/{}] loss:{:.3f}, acc:{:.3f}".format(epoch + 1, epochs, train_loss.result(), train_accuracy.result()) # validate val_bar = tqdm(range(total_val // batch_size), colour='green') for step in val_bar: val_images, val_labels = next(val_data_gen) val_step(val_images, val_labels) # print val process val_bar.desc = "valid epoch[{}/{}] loss:{:.3f}, acc:{:.3f}".format(epoch + 1, epochs, val_loss.result(), val_accuracy.result()) if val_loss.result() < best_test_loss: best_test_loss = val_loss.result() model.save_weights("./save_weights/resMobileNetV2.ckpt", save_format="tf")
AUTOTUNE = tf.data.experimental.AUTOTUNE # load train dataset train_dataset = tf.data.Dataset.from_tensor_slices((train_image_list, train_label_list)) train_dataset = train_dataset.shuffle(buffer_size=train_num)\ .map(process_train_img, num_parallel_calls=AUTOTUNE)\ .repeat().batch(batch_size).prefetch(AUTOTUNE) # load train dataset val_dataset = tf.data.Dataset.from_tensor_slices((val_image_list, val_label_list)) val_dataset = val_dataset.map(process_val_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)\ .repeat().batch(batch_size) # 实例化模型 model = MobileNetV2(num_classes=5) model.summary() # using keras low level api for training loss_object = tf.keras.losses.CategoricalCrossentropy(from_logits=True) optimizer = tf.keras.optimizers.Adam(learning_rate=0.0005) train_loss = tf.keras.metrics.Mean(name='train_loss') train_accuracy = tf.keras.metrics.CategoricalAccuracy(name='train_accuracy') test_loss = tf.keras.metrics.Mean(name='test_loss') test_accuracy = tf.keras.metrics.CategoricalAccuracy(name='test_accuracy') @tf.function def train_step(images, labels):
def main(): """ 主程序 :return: """ num_cls = args.wave_num * args.k + 1 # 所有的路由和波长选择组合,加上啥都不选 action_shape = 1 # action的维度,默认是1. num_updates = int( args.steps) // args.workers // args.num_steps # 梯度一共需要更新的次数 if args.append_route.startswith("True"): channel_num = args.wave_num + args.k else: channel_num = args.wave_num # 解析weight if args.weight.startswith('None'): weight = None else: weight = args.weight # 创建actor_critic if args.mode.startswith('alg'): # ksp(args, weight) return elif args.mode.startswith('learning'): # CNN学习模式下,osb的shape应该是CHW obs_shape = (channel_num, args.img_height, args.img_width) if args.cnn.startswith('mobilenetv2'): actor_critic = MobileNetV2(in_channels=channel_num, num_classes=num_cls, t=6) elif args.cnn.startswith('simplenet'): actor_critic = SimpleNet(in_channels=channel_num, num_classes=num_cls) elif args.cnn.startswith('simplestnet'): actor_critic = SimplestNet(in_channels=channel_num, num_classes=num_cls) elif args.cnn.startswith('alexnet'): actor_critic = AlexNet(in_channels=channel_num, num_classes=num_cls) elif args.cnn.startswith('squeezenet'): actor_critic = SqueezeNet(in_channels=channel_num, num_classes=num_cls, version=1.0) elif args.cnn.startswith('expandsimplenet'): actor_critic = ExpandSimpleNet(in_channels=channel_num, num_classes=num_cls, expand_factor=args.expand_factor) elif args.cnn.startswith('deepersimplenet'): actor_critic = DeeperSimpleNet(in_channels=channel_num, num_classes=num_cls, expand_factor=args.expand_factor) else: raise NotImplementedError # 创建optimizer if args.algo.startswith("a2c"): optimizer = optim.RMSprop(actor_critic.parameters(), lr=args.base_lr, eps=args.epsilon, alpha=args.alpha) elif args.algo.startswith("ppo"): optimizer = optim.Adam(actor_critic.parameters(), lr=args.base_lr, eps=args.epsilon) else: raise NotImplementedError else: raise NotImplementedError if args.cuda.startswith("True"): # 如果要使用cuda进行计算 actor_critic.cuda() # actor_critic = DistModule(actor_critic) # 判断是否是评估模式 if args.evaluate: print("evaluate mode") models = {} times = 1 prefix = "trained_models" directory = os.path.join(prefix, 'a2c', args.cnn, args.step_over) env = RwaGame(net_config=args.net, wave_num=args.wave_num, rou=args.rou, miu=args.miu, max_iter=args.max_iter, k=args.k, mode=args.mode, img_width=args.img_width, img_height=args.img_height, weight=weight, step_over=args.step_over) for model_file in reversed( sorted(os.listdir(directory), key=lambda item: int(item.split('.')[0]))): model_file = os.path.join(directory, model_file) print("evaluate model {}".format(model_file)) params = torch.load(model_file) actor_critic.load_state_dict(params['state_dict']) actor_critic.eval() models[params['update_i']] = {} print("model loading is finished") for t in range(times): total_reward, total_services, allocated_services = 0, 0, 0 obs, reward, done, info = env.reset() while not done: inp = Variable(torch.Tensor(obs).unsqueeze(0), volatile=True) # 禁止梯度更新 value, action, action_log_prob = actor_critic.act( inputs=inp, deterministic=True) # 确定性决策 action = action.data.numpy()[0] obs, reward, done, info = env.step(action=action[0]) total_reward += reward if reward == ARRIVAL_NEWPORT or reward == ARRIVAL_NOPORT: allocated_services += 1 if args.step_over.startswith('one_time'): if info: total_services += 1 elif args.step_over.startswith('one_service'): total_services += 1 else: raise NotImplementedError models[params['update_i']]['time'] = t models[params['update_i']]['reward'] = total_reward models[params['update_i']]['total_services'] = total_services models[params['update_i']][ 'allocated_services'] = allocated_services models[params['update_i']]['bp'] = ( total_services - allocated_services) / total_services # 输出仿真结果 # print("|updated model|test index|reward|bp|total services|allocated services|") # print("|:-----|:-----|:-----|:-----|:-----|:-----|") # for m in sorted(models): for i in range(times): print("|{up}|{id}|{r}|{bp:.4f}|{ts}|{als}|".format( up=params['update_i'], id=models[params['update_i']]['time'], r=models[params['update_i']]['reward'], bp=models[params['update_i']]['bp'], ts=models[params['update_i']]['total_services'], als=models[params['update_i']]['allocated_services'])) return # 创建游戏环境 envs = [ make_env(net_config=args.net, wave_num=args.wave_num, k=args.k, mode=args.mode, img_width=args.img_width, img_height=args.img_height, weight=weight, step_over=args.step_over) for _ in range(args.workers) ] envs = SubprocEnv(envs) # 创建游戏运行过程中相关变量存储更新的容器 rollout = RolloutStorage(num_steps=args.num_steps, num_processes=args.workers, obs_shape=obs_shape, action_shape=action_shape) current_obs = torch.zeros(args.workers, *obs_shape) observation, _, _, _ = envs.reset() update_current_obs(current_obs, observation, channel_num) rollout.observations[0].copy_(current_obs) # These variables are used to compute average rewards for all processes. episode_rewards = torch.zeros([args.workers, 1]) final_rewards = torch.zeros([args.workers, 1]) if args.cuda.startswith("True"): current_obs = current_obs.cuda() rollout.cuda() start = time.time() log_start = time.time() total_services = 0 # log_interval期间一共有多少个业务到达 allocated_services = 0 # log_interval期间一共有多少个业务被分配成功 update_begin = 0 # 判断是否是接续之前的训练 if args.resume: pms = torch.load(args.resume) actor_critic.load_state_dict(pms['state_dict']) optimizer.load_state_dict(pms['optimizer']) update_begin = pms['update_i'] print("resume process from update_i {}, with base_lr {}".format( update_begin, args.base_lr)) for updata_i in range(update_begin, num_updates): update_start = time.time() for step in range(args.num_steps): # 选择行为 inp = Variable(rollout.observations[step], volatile=True) # 禁止梯度更新 value, action, action_log_prob = actor_critic.act( inputs=inp, deterministic=False) # print(action) # 压缩维度,放到cpu上执行。因为没有用到GPU,所以并没有什么卵用,权当提示 cpu_actions = action.data.squeeze(1).cpu().numpy() # 观察observation,以及下一个observation envs.step_async(cpu_actions) obs, reward, done, info = envs.step_wait( ) # reward和done都是(n,)的numpy.ndarray向量 # if reward == ARRIVAL_NEWPORT_NEWPORT or reward == ARRIVAL_NOPORT_NEWPORT or reward == ARRIVAL_NOPORT_NOPORT: # allocated_services += 1 print(reward) for i in reward: if i == ARRIVAL_NEWPORT or i == ARRIVAL_NOPORT: allocated_services += 1 # allocated_services += (reward==ARRIVAL_NEWPORT_NEWPORT or reward==ARRIVAL_NOPORT_NEWPORT or reward==ARRIVAL_NOPORT_NOPORT).any().sum() # 计算分配成功的reward的次数 # TODO 未解决 if args.step_over.startswith('one_service'): total_services += (info == True).sum() # 计算本次step中包含多少个业务到达事件 # elif args.step_over.startswith('one_service'): # total_services += args.workers else: raise NotImplementedError reward = torch.from_numpy(np.expand_dims(reward, 1)).float() episode_rewards += reward # 累加reward分数 # 如果游戏结束,则重新开始计算episode_rewards和final_rewards,并且以返回的reward为初始值重新进行累加。 masks = torch.FloatTensor([[0.0] if d else [1.0] for d in done ]) # True --> 0, False --> 1 final_rewards *= masks final_rewards += (1 - masks) * episode_rewards episode_rewards *= masks # if done[len(done)-1]: # print('游戏结束最终端口数量:',envs.get_all_edges_port()) if args.cuda.startswith("True"): masks = masks.cuda() # 给masks扩充2个维度,与current_obs相乘。则运行结束的游戏进程对应的obs值会变成0,图像上表示全黑,即游戏结束的画面。 current_obs *= masks.unsqueeze(2).unsqueeze(2) update_current_obs(current_obs=current_obs, obs=obs, channel_num=channel_num) # 把本步骤得到的结果存储起来 rollout.insert(step=step, current_obs=current_obs, action=action.data, action_log_prob=action_log_prob.data, value_pred=value.data, reward=reward, mask=masks) # TODO 强行停止 # envs.close() # return # 注意不要引用上述for循环定义的变量。下面变量的命名和使用都要注意。 next_inp = Variable(rollout.observations[-1], volatile=True) # 禁止梯度更新 next_value = actor_critic(next_inp)[0].data # 获取下一步的value值 rollout.compute_returns(next_value=next_value, use_gae=False, gamma=args.gamma, tau=None) if args.algo.startswith('a2c'): # 下面进行A2C算法梯度更新 inps = Variable(rollout.observations[:-1].view(-1, *obs_shape)) acts = Variable(rollout.actions.view(-1, action_shape)) # print("a2cs's acts size is {}".format(acts.size())) value, action_log_probs, cls_entropy = actor_critic.evaluate_actions( inputs=inps, actions=acts) print(cls_entropy.data) # print("inputs' shape is {}".format(inps.size())) # print("value's shape is {}".format(value.size())) value = value.view(args.num_steps, args.workers, 1) # print("action_log_probs's shape is {}".format(action_log_probs.size())) action_log_probs = action_log_probs.view(args.num_steps, args.workers, 1) # 计算loss advantages = Variable(rollout.returns[:-1]) - value value_loss = advantages.pow(2).mean() # L2Loss or MSE Loss action_loss = -(Variable(advantages.data) * action_log_probs).mean() total_loss = value_loss * args.value_loss_coef + action_loss - cls_entropy * args.entropy_coef optimizer.zero_grad() total_loss.backward() # 下面进行迷之操作。。梯度裁剪(https://www.cnblogs.com/lindaxin/p/7998196.html) nn.utils.clip_grad_norm(actor_critic.parameters(), args.max_grad_norm) # average_gradients(actor_critic) optimizer.step() elif args.algo.startswith('ppo'): # 下面进行PPO算法梯度更新 advantages = rollout.returns[:-1] - rollout.value_preds[:-1] advantages = (advantages - advantages.mean()) / (advantages.std() + 1e-5) for e in range(args.ppo_epoch): data_generator = rollout.feed_forward_generator( advantages, args.num_mini_batch) for sample in data_generator: observations_batch, actions_batch, \ return_batch, masks_batch, old_action_log_probs_batch, \ adv_targ = sample # Reshape to do in a single forward pass for all steps values, action_log_probs, cls_entropy = actor_critic.evaluate_actions( Variable(observations_batch), Variable(actions_batch)) adv_targ = Variable(adv_targ) ratio = torch.exp(action_log_probs - Variable(old_action_log_probs_batch)) surr1 = ratio * adv_targ surr2 = torch.clamp(ratio, 1.0 - args.clip_param, 1.0 + args.clip_param) * adv_targ action_loss = -torch.min( surr1, surr2).mean() # PPO's pessimistic surrogate (L^CLIP) value_loss = (Variable(return_batch) - values).pow(2).mean() # 事后一支烟 rollout.after_update() update_time = time.time() - update_start print("updates {} finished, cost time {}:{}".format( updata_i, update_time // 60, update_time % 60)) # print("total services is {}".format(total_services)) # 存储模型 if updata_i % args.save_interval == 0: save_path = os.path.join(args.save_dir, 'a2c') save_path = os.path.join(save_path, args.cnn) save_path = os.path.join(save_path, args.step_over) save_path = os.path.join(save_path, args.parameter) if os.path.exists(save_path) and os.path.isdir(save_path): pass else: os.makedirs(save_path) save_file = os.path.join(save_path, str(updata_i) + '.tar') save_content = { 'update_i': updata_i, 'state_dict': actor_critic.state_dict(), 'optimizer': optimizer.state_dict(), 'mean_reward': final_rewards.mean() } torch.save(save_content, save_file) # 输出日志 if updata_i % args.log_interval == 0: end = time.time() interval = end - log_start remaining_seconds = (num_updates - updata_i - 1) / args.log_interval * interval remaining_hours = int(remaining_seconds // 3600) remaining_minutes = int((remaining_seconds % 3600) / 60) total_num_steps = (updata_i + 1) * args.workers * args.num_steps blocked_services = total_services - allocated_services bp = blocked_services / total_services wave_port_num, total_port_num = envs.get_all_edges_port() wave_occ_sum, resource_utilization_rate = envs.get_resourceUtilization( ) print( "Updates {}, num timesteps {}, FPS {}, mean/median reward {:.1f}/{:.1f}, min/max reward {:.1f}/{:.1f}, \ entropy {:.5f}, value loss {:.5f}, policy loss {:.8f}, remaining time {}:{}, 阻塞率为{}/{}={}, \ 各个波长端口数量为{}, 总的端口数量为{}, 带宽占用情况为{}, 资源占用率为{}".format( updata_i, total_num_steps, int(total_num_steps / (end - start)), final_rewards.mean(), final_rewards.median(), final_rewards.min(), final_rewards.max(), cls_entropy.data, value_loss.data, action_loss.data, remaining_hours, remaining_minutes, blocked_services, total_services, bp, wave_port_num, total_port_num, wave_occ_sum, resource_utilization_rate)) # raise NotImplementedError total_services = 0 allocated_services = 0 log_start = time.time() envs.close()
elif args.enc_model == "resnet": print("ResNet Model") from model import ResNetModel model = ResNetModel(train_enc=bool(args.train_enc), load_weight=args.load_weight) elif args.enc_model == "vgg": print("VGG Model") from model import VGGModel model = VGGModel(train_enc=bool(args.train_enc), load_weight=args.load_weight) elif args.enc_model == "mobilenet": print("Mobile NetV2") from model import MobileNetV2 model = MobileNetV2(train_enc=bool(args.train_enc), load_weight=args.load_weight) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') if torch.cuda.device_count() > 1: print("Let's use", torch.cuda.device_count(), "GPUs!") model = nn.DataParallel(model) model.to(device) train_img_ids = [nm.split(".")[0] for nm in os.listdir(train_img_dir)] val_img_ids = [nm.split(".")[0] for nm in os.listdir(val_img_dir)] train_dataset = SaliconDataset(train_img_dir, train_gt_dir, train_fix_dir, train_img_ids) val_dataset = SaliconDataset(val_img_dir, val_gt_dir, val_fix_dir, val_img_ids) train_loader = torch.utils.data.DataLoader(train_dataset,
def main(): # parse arguments args = parse_args() if args.cpu: os.environ["CUDA_VISIBLE_DEVICES"] = "" sess = tf.Session() if args.is_train: # read tfrecord files glob_pattern = os.path.join(args.dataset_dir, '*.tfrecord') tfrecords_list = glob.glob(glob_pattern) # check dirs if not os.path.exists(args.checkpoint_dir): os.makedirs(args.checkpoint_dir) if not os.path.exists(args.logs_dir): os.makedirs(args.logs_dir) model = MobileNetV2(sess=sess, tf_files=tfrecords_list, num_sampes=args.num_samples, epoch=args.epoch, batch_size=args.batch_size, image_height=args.image_height, image_width=args.image_width, n_classes=args.n_classes, is_train=args.is_train, learning_rate=args.learning_rate, lr_decay=args.lr_decay, beta1=args.beta1, chkpt_dir=args.checkpoint_dir, logs_dir=args.logs_dir, model_name=args.model_name, rand_crop=args.rand_crop) model._build_train_graph() model._train() else: # restore model saver = tf.train.import_meta_graph( os.path.join(args.checkpoint_dir, args.model_name + '-200.meta')) saver.restore(sess, tf.train.latest_checkpoint(args.checkpoint_dir)) # get input and output tensors from graph graph = tf.get_default_graph() input_x = graph.get_tensor_by_name("input:0") input_y = graph.get_tensor_by_name("label:0") prob = graph.get_tensor_by_name("mobilenetv2/prob:0") # prepare eval/test data and label img = imread('data/tmp/art01.jpg') img = preprocess(img) img = imresize(img, (args.image_height, args.image_width)) label = 1 feed_dict = { input_x: [img], input_y: [label] } # use [], because we need 4-D tensor start = time.time() res = sess.run(prob, feed_dict=feed_dict)[0] # index 0 for batch_size print('prob: {}'.format(res)) print('time: {}'.format(time.time() - start))
def main(): global args, best_prec1 args = parser.parse_args() args.distributed = args.world_size > 1 if args.distributed: dist.init_process_group(backend=args.dist_backend, init_method=args.dist_url, world_size=args.world_size) # create model 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)) if args.arch.startswith('mobilenetv2'): image_size = int(args.input_size*1.4) input_size = args.input_size model = MobileNetV2(input_size=input_size,width_multi=args.width_multi,expansion=args.expansion) else: model = models.__dict__[args.arch]() print(model) if not args.distributed: 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() else: model.cuda() model = torch.nn.parallel.DistributedDataParallel(model) # define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss().cuda() optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay) if args.evaluate: if os.path.isfile(args.evaluate): print("=> loading model '{}'".format(args.evaluate)) model.load_state_dict(torch.load(args.evaluate)) else: print("=> no model found at '{}'".format(args.evaluate)) # 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_prec1 = checkpoint['best_prec1'] model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) print("=> loaded checkpoint '{}' (epoch {})" .format(args.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(args.resume)) cudnn.benchmark = True normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) train_dataset = ImageNetDataset( args.train_root, args.train_source, transforms.Compose([ transforms.RandomResizedCrop(input_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize, ])) val_dataset = ImageNetDataset( args.val_root, args.val_source, transforms.Compose([ transforms.Resize(image_size), transforms.CenterCrop(input_size), 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) val_loader = torch.utils.data.DataLoader( val_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=False) if args.evaluate: validate(val_loader, model, criterion) return for epoch in range(args.start_epoch, args.epochs): if args.distributed: train_sampler.set_epoch(epoch) adjust_learning_rate(optimizer, epoch) # train for one epoch train(train_loader, model, criterion, optimizer, epoch) # evaluate on validation set prec1 = validate(val_loader, model, criterion) # remember best prec@1 and save checkpoint is_best = prec1 > best_prec1 best_prec1 = max(prec1, best_prec1) save_checkpoint({ 'epoch': epoch + 1, 'arch': args.arch, 'state_dict': model.state_dict(), 'best_prec1': best_prec1, 'optimizer': optimizer.state_dict(), }, is_best, args.save_path)
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) data_root = os.path.abspath(os.path.join(os.getcwd(), "../..")) # get data root path image_path = data_root + "/data_set/flower_data/" # flower data set path validate_dataset = datasets.ImageFolder(root=image_path + "val", transform=data_transform) batch_size = 16 validate_loader = torch.utils.data.DataLoader(validate_dataset, batch_size=batch_size, shuffle=False, num_workers=2) net = MobileNetV2(num_classes=5) # load pretrain weights model_weight_path = "./MobileNetV2.pth" net.load_state_dict(torch.load(model_weight_path, map_location=device)) net.to(device) # read class_indict try: json_file = open('./class_indices.json', 'r') class_indict = json.load(json_file) except Exception as e: print(e) exit(-1) labels = [label for _, label in class_indict.items()] confusion = ConfusionMatrix(num_classes=5, labels=labels)
def main(args): print(args) np.random.seed(args.seed) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') train_data_transforms = tv.transforms.Compose([ tv.transforms.Resize((args.image_size, args.image_size)), tv.transforms.RandomCrop(args.image_size, 4), tv.transforms.RandomHorizontalFlip(), tv.transforms.RandomRotation((-45, 45)), tv.transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2), tv.transforms.ToTensor(), tv.transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) ]) test_data_transforms = tv.transforms.Compose([ tv.transforms.Resize((args.image_size, args.image_size)), tv.transforms.ToTensor(), tv.transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) ]) train_dataset = DataSet(args.file_list, args.image_channels, args.image_size, transforms=train_data_transforms, is_train=True) test_dataset = DataSet(args.test_list, args.image_channels, args.image_size, transforms=test_data_transforms, is_train=False) train_loader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=12) test_loader = DataLoader(test_dataset, batch_size=args.batch_size, shuffle=True, num_workers=12) model_dir = args.model_dir print(model_dir) if not os.path.exists(model_dir): os.mkdir(model_dir) print('Total number of examples: {}'.format(len(train_dataset))) print('Test number of examples: {}'.format(len(test_dataset))) print('Model dir: {}'.format(model_dir)) # model = ONet(nums_class=args.nums_class) # model = PNet(nums_class=args.nums_class) model = MobileNetV2(input_channels=3, nums_class=args.nums_class) # model = BlazeLandMark(nums_class=args.nums_class) if args.pretrained_model: pretrained_model = args.pretrained_model if args.all_model: print('load all model, model graph and weight included!') if not os.path.isdir(pretrained_model): print('Restoring pretrained model: {}'.format(pretrained_model)) model = torch.load(pretrained_model) else: print('Model directory: {}'.format(pretrained_model)) files = os.listdir(pretrained_model) assert len(files) == 1 and files[0].split('.')[-1] in ['pt', 'pth'] model_path = os.path.join(pretrained_model, files[0]) print('Model name:{}'.format(files[0])) model = torch.load(model_path) else: if not os.path.isdir(pretrained_model): print('Restoring pretrained model: {}'.format(pretrained_model)) model.load_state_dict(torch.load(pretrained_model)) else: print('Model directory: {}'.format(pretrained_model)) files = os.listdir(pretrained_model) assert len(files) == 1 and files[0].split('.')[-1] in ['pt', 'pth'] model_path = os.path.join(pretrained_model, files[0]) print('Model name:{}'.format(files[0])) model.load_state_dict(torch.load(model_path)) test(test_loader, model, args, device) model.to(device) optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate, weight_decay=args.weight_decay) # optimizer lr_epoch = args.lr_epoch.strip().split(',') lr_epoch = list(map(int, lr_epoch)) scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=lr_epoch, gamma=0.1) criterion = nn.CrossEntropyLoss() print('Running train.') start_time = time.time() for epoch in range(args.max_epoch): model.train() scheduler.step(epoch) correct = 0. total = 0 for i_batch, (images_batch, labels_batch) in enumerate(train_loader): images_batch = Variable(images_batch.to(device)) labels_batch = Variable(labels_batch.to(device)) # labels_batch = torch.zeros(args.batch_size, args.nums_class).scatter_(1, labels_batch, 1) # labels_batch = labels_batch.to(device, dtype=torch.int64) pre_labels = model(images_batch) #print('...............',pre_labels) print(pre_labels.size(), labels_batch.size(), labels_batch.squeeze()) loss = criterion(pre_labels, labels_batch.squeeze(axis=1)) optimizer.zero_grad() loss.backward() optimizer.step() if ((i_batch + 1) % 100) == 0 or (i_batch + 1) == len(train_loader): Epoch = 'Epoch:[{:<4}][{:<4}/{:<4}]'.format(epoch, i_batch + 1, len(train_loader)) Loss = 'Loss: {:2.3f}'.format(loss.item()) trained_sum_iters = len(train_loader) * epoch + i_batch + 1 average_time = (time.time() - start_time) / trained_sum_iters remain_time = average_time * (len(train_loader) * args.max_epoch - trained_sum_iters) / 3600 print('{}\t{}\t lr {:2.3}\t average_time:{:.3f}s\t remain_time:{:.3f}h'.format(Epoch, Loss, optimizer.param_groups[0]['lr'], average_time, remain_time)) _, predicted = torch.max(pre_labels.data, 1) total += labels_batch.size(0) correct += (predicted == labels_batch[:, 0]).sum() # save model checkpoint_path = os.path.join(model_dir, 'model_'+str(epoch)+'.pth') if args.all_model: torch.save(model, checkpoint_path) else: torch.save(model.state_dict(), checkpoint_path) print('Train Images: {}, right nums: {}, right rate:{:.3f}%'.format(total, correct, correct.to(torch.float32) * 100 / total)) test(test_loader, model, device)