def __init__(self, data_dir, transformation, device, num_bin): args.data_dir = data_dir args.filename_list = data_dir args.dataset = 'AFLW2000' self.transformations = transformation self.device = device self.num_bin = num_bin idx_tensor = [idx for idx in xrange(67)] self.idx_tensor = torch.FloatTensor(idx_tensor).to(self.device) if args.dataset == 'Pose_300W_LP': pose_dataset = datasets.Pose_300W_LP(args.data_dir, args.filename_list, self.transformations) elif args.dataset == 'Pose_300W_LP_random_ds': pose_dataset = datasets.Pose_300W_LP_random_ds( args.data_dir, args.filename_list, self.transformations) elif args.dataset == 'AFLW2000': pose_dataset = datasets.AFLW2000(args.data_dir, args.filename_list, self.transformations) elif args.dataset == 'AFLW2000_ds': pose_dataset = datasets.AFLW2000_ds(args.data_dir, args.filename_list, self.transformations) elif args.dataset == 'BIWI': pose_dataset = datasets.BIWI(args.data_dir, args.filename_list, self.transformations) elif args.dataset == 'AFLW': pose_dataset = datasets.AFLW(args.data_dir, args.filename_list, self.transformations) elif args.dataset == 'AFLW_aug': pose_dataset = datasets.AFLW_aug(args.data_dir, args.filename_list, self.transformations) elif args.dataset == 'AFW': pose_dataset = datasets.AFW(args.data_dir, args.filename_list, self.transformations) else: print 'Error: not a valid dataset name' sys.exit() self.test_loader = torch.utils.data.DataLoader( dataset=pose_dataset, batch_size=args.batch_size, num_workers=2)
transforms.Scale(224), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # rgb模式 if args.dataset == 'Pose_300W_LP': pose_dataset = datasets.Pose_300W_LP(args.data_dir, args.filename_list, transformations) elif args.dataset == 'Pose_300W_LP_random_ds': pose_dataset = datasets.Pose_300W_LP_random_ds(args.data_dir, args.filename_list, transformations) elif args.dataset == 'AFLW2000': pose_dataset = datasets.AFLW2000(args.data_dir, args.filename_list, transformations) elif args.dataset == 'AFLW2000_ds': pose_dataset = datasets.AFLW2000_ds(args.data_dir, args.filename_list, transformations) elif args.dataset == 'BIWI': pose_dataset = datasets.BIWI(args.data_dir, args.filename_list, transformations) elif args.dataset == 'AFLW': pose_dataset = datasets.AFLW(args.data_dir, args.filename_list, transformations) elif args.dataset == 'AFLW_aug': pose_dataset = datasets.AFLW_aug(args.data_dir, args.filename_list, transformations) elif args.dataset == 'AFW': pose_dataset = datasets.AFW(args.data_dir, args.filename_list, transformations)
load_filtered_state_dict(model, saved_state_dict) print 'Loading data.' transformations = transforms.Compose([transforms.Scale(240), transforms.RandomCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])]) if args.dataset == 'Pose_300W_LP': pose_dataset = datasets.Pose_300W_LP(args.data_dir, args.filename_list, transformations, bin_width_degrees) elif args.dataset == 'Pose_300W_LP_random_ds': pose_dataset = datasets.Pose_300W_LP_random_ds(args.data_dir, args.filename_list, transformations, bin_width_degrees) elif args.dataset == 'Synhead': pose_dataset = datasets.Synhead(args.data_dir, args.filename_list, transformations, bin_width_degrees) elif args.dataset == 'AFLW2000': pose_dataset = datasets.AFLW2000(args.data_dir, args.filename_list, transformations, bin_width_degrees) elif args.dataset == 'BIWI': pose_dataset = datasets.BIWI(args.data_dir, args.filename_list, transformations, bin_width_degrees) elif args.dataset == 'AFLW': pose_dataset = datasets.AFLW(args.data_dir, args.filename_list, transformations, bin_width_degrees) elif args.dataset == 'AFLW_aug': pose_dataset = datasets.AFLW_aug(args.data_dir, args.filename_list, transformations, bin_width_degrees) elif args.dataset == 'AFW': pose_dataset = datasets.AFW(args.data_dir, args.filename_list, transformations, bin_width_degrees) else: print 'Error: not a valid dataset name' sys.exit() train_loader = torch.utils.data.DataLoader(dataset=pose_dataset, batch_size=batch_size, shuffle=True,
def test(): args = parse_args() os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id mae_yaw = 0 mae_pitch = 0 mae_roll = 0 images = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3], name='image_batch') is_training = tf.placeholder(tf.bool) num_bins = 66 keep_prob = 0 with slim.arg_scope(nets.resnet_v1.resnet_arg_scope): net, endpoints = nets.resnet_v1.resnet_v1_50(images, num_classes=None, is_training=is_training) with tf.variable_scope('Logits'): net = tf.squeeze(net, axis=[1, 2]) net = slim.dropout(net, keep_prob, scope='scope') yaw = slim.fully_connected(net, num_outputs=num_bins, activation_fn=None, scope='fc_yaw') pitch = slim.fully_connected(net, num_outputs=num_bins, activation_fn=None, scope='fc_pitch') roll = slim.fully_connected(net, num_outputs=num_bins, activation_fn=None, scope='fc_roll') yaw_predicted = tf.nn.softmax(yaw) pitch_predicted = tf.nn.softmax(pitch) roll_predicted = tf.nn.softmax(roll) idx_tensor = [idx for idx in range(66)] idx_tensor = tf.convert_to_tensor(idx_tensor, tf.float32) yaw_predicted = tf.reduce_sum(yaw_predicted * idx_tensor, 1) * 3 - 99 pitch_predicted = tf.reduce_sum(pitch_predicted * idx_tensor, 1) * 3 - 99 roll_predicted = tf.reduce_sum(roll_predicted * idx_tensor, 1) * 3 - 99 config = tf.ConfigProto(log_device_placement=False, allow_soft_placement=True) config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: saver = tf.train.Saver() ckpt = tf.train.latest_checkpoint(args.checkpoint_dir) saver.restore(sess, ckpt) test_data = datasets.AFLW2000() for i in range(test_data.length): img = test_data[i][0] img = img.resize((224, 224), Image.BILINEAR) precesssed_img = datasets.nomalizing(img, [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) b_img = tf.expand_dims(precesssed_img, 0) b_img = b_img.eval() feed_dict = {images: b_img, is_training: False} pre_yaw, pre_pitch, pre_roll = sess.run( [yaw_predicted, pitch_predicted, roll_predicted], feed_dict=feed_dict) cont_labels = test_data[i][2] yaw_label = cont_labels[0] pitch_label = cont_labels[1] roll_label = cont_labels[2] mae_yaw = mae_yaw + abs(pre_yaw - yaw_label) mae_pitch = mae_pitch + abs(pre_pitch - pitch_label) mae_roll = mae_roll + abs(pre_roll - roll_label) mean_yaw = mae_yaw / test_data.length mean_pitch = mae_pitch / test_data.length mean_roll = mae_roll / test_data.length return mean_yaw, mean_pitch, mean_roll
if args.dataset == 'Pose_300W_LP': pose_dataset = datasets.Pose_300W_LP(args.data_dir, args.filename_list, transformations) elif args.dataset == 'Pose_300W_LP_random_ds': pose_dataset = datasets.Pose_300W_LP_random_ds(args.data_dir, args.filename_list, transformations) elif args.dataset == 'Synhead': pose_dataset = datasets.Synhead(args.data_dir, args.filename_list, transformations) elif args.dataset == 'AFLW2000': pose_dataset = datasets.AFLW2000(args.data_dir, args.filename_list, transformations) elif args.dataset == 'BIWI': pose_dataset = datasets.BIWI(args.data_dir, args.filename_list, transformations) elif args.dataset == 'AFLW': pose_dataset = datasets.AFLW(args.data_dir, args.filename_list, transformations) elif args.dataset == 'AFLW_aug': pose_dataset = datasets.AFLW_aug(args.data_dir, args.filename_list, transformations) elif args.dataset == 'AFW': pose_dataset = datasets.AFW(args.data_dir, args.filename_list,
transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])]) data_dir = os.path.join('Data\\RAW', args.data_dir) vdata_dir = os.path.join('Data\\RAW', args.vdata_dir) if args.dataset == 'Pose_300W_Training': pose_dataset = datasets.Pose_300W_Training(data_dir, args.filename_list, transformations, max_angle=max_angle, bin_angle=angles_per_bin) elif args.dataset == 'Pose_300W_PNP_Training': pose_dataset = datasets.Pose_300W_PNP_Training(data_dir, args.filename_list, transformations, max_angle=max_angle, bin_angle=angles_per_bin) else: print('Error: not a valid dataset name') sys.exit() if args.vdataset == 'AFLW2000': validate_pose_dataset = datasets.AFLW2000(vdata_dir, args.vfilename_list, vtransformations) else: print('Error: not a valid validation dataset name') sys.exit() train_loader = torch.utils.data.DataLoader(dataset=pose_dataset, batch_size=batch_size, shuffle=True, num_workers=2) validation_loader = torch.utils.data.DataLoader(dataset=validate_pose_dataset, batch_size=1, num_workers=2) model.cuda(gpu) criterion = nn.CrossEntropyLoss().cuda(gpu)
if args.dataset == 'Pose_300W_LP': train_filename_list = os.path.join(args.filename_list, 'train_filename_all.npy') val_filename_list = os.path.join(args.filename_list, 'val_filename_all.npy') pose_dataset_train = datasets.Pose_300W_LP(args.data_dir, num_bins, train_filename_list, transformations, args.debug) pose_dataset_val = datasets.Pose_300W_LP(args.data_dir, num_bins, val_filename_list, transformations, args.debug) elif args.dataset == 'AFLW2000': test_filename_list = os.path.join(args.filename_list, 'test_filename.npy') pose_dataset = datasets.AFLW2000(args.data_dir, num_bins, args.filename_list, transformations) else: print('Error: not a valid dataset name') sys.exit() train_loader = torch.utils.data.DataLoader(dataset=pose_dataset_train, batch_size=batch_size, shuffle=True, num_workers=4) val_loader = torch.utils.data.DataLoader(dataset=pose_dataset_val, batch_size=batch_size, shuffle=True, num_workers=4) ## Handling Class Imbalance yaw_l = []
yaw = Dense(units=BIN_NUM, name='yaw')(feature) pitch = Dense(units=BIN_NUM, name='pitch')(feature) roll = Dense(units=BIN_NUM, name='roll')(feature) model = Model(inputs=inputs, outputs=[yaw, pitch, roll]) model.compile(optimizer='adam', loss={ 'yaw': loss_angle, 'pitch': loss_angle, 'roll': loss_angle, }) # model.load_weights(BIWI_PRETRAINED) aflw2000 = datasets.AFLW2000(AFLW2000_DATA_DIR, '/filename_list.txt', batch_size=BATCH_SIZE, input_size=INPUT_SIZE) model.summary() model.fit(x=aflw2000.data_generator(), epochs=EPOCHS, steps_per_epoch=aflw2000.train_num // BATCH_SIZE, max_queue_size=10, workers=1, verbose=1) model.save(MODELS_DIR + "hopenet2.h5")