Ejemplo n.º 1
0
def run_main():
    """
       这是主函数
    """
    # 初始化参数配置类
    pre_model_path = os.path.abspath("xxxx")
    cfg = config(pre_model_path=pre_model_path)

    # 构造训练集和测试集数据生成器
    #dataset_dir = os.path.join(cfg.dataset_dir, "train")
    dataset_dir = os.path.join(cfg.dataset_dir, "test")
    image_data = K.preprocessing.image.ImageDataGenerator()
    datagen = image_data.flow_from_directory(dataset_dir,
                                             class_mode='categorical',
                                             batch_size=1,
                                             target_size=(cfg.image_size,
                                                          cfg.image_size),
                                             shuffle=False)

    # 初始化相关参数
    iter_num = datagen.samples  # 训练集1个epoch的迭代次数

    # 初始化VGG16,并进行训练
    vgg16 = VGG16(cfg)
    vgg16.eval_on_dataset(datagen, iter_num, weight_path=cfg.pre_model_path)
Ejemplo n.º 2
0
def selector(args):
    if args == 'lenet':
        return LeNet5()
    elif args == 'mobilenet':
        return MobileNet()
    elif args == 'mobilenetv2':
        return MobileNetV2()
    elif args == 'vgg16':
        return VGG16()
    elif args == 'resnet50':
        return ResNet50()
Ejemplo n.º 3
0
def run_main():
    """
       这是主函数
    """
    # 初始化参数配置类
    pre_model_path = os.path.abspath("xxxx")
    cfg = config(pre_model_path=pre_model_path)

    # 初始化图像与结果路径
    image_path = os.path.abspath("./test/10405299_1.png")

    # 初始化VGG16,并进行测试一张图像
    print(cfg.pre_model_path)
    vgg16 = VGG16(cfg)
    vgg16.test_single_image(image_path, cfg.pre_model_path)
Ejemplo n.º 4
0
def run_main():
    """
       这是主函数
    """
    # 初始化参数配置类
    pre_model_path = os.path.abspath(
        "./pre_weights/20200320070419/Epoch004_ val_loss_0.693,val_accuracy_100.000%.ckpt"
    )
    cfg = config(pre_model_path=pre_model_path)

    # 初始化图像与结果路径
    image_dir = os.path.abspath("./test")

    # 初始化UNet,并进行训练
    print(cfg.pre_model_path)
    vgg16 = VGG16(cfg)
    vgg16.test_single_image(image_dir, cfg.pre_model_path)
Ejemplo n.º 5
0
def run_main():
    """
       这是主函数
    """
    # 初始化参数配置类
    pre_model_path = os.path.abspath("./pre_weight/vgg16_weights_tf_dim_ordering_tf_kernels.h5")
    cfg = config(pre_model_path=pre_model_path)

    # 构造训练集和测试集数据生成器
    train_dataset_dir = os.path.join(cfg.dataset_dir,"train")
    val_dataset_dir = os.path.join(cfg.dataset_dir,"val")
    image_data =  K.preprocessing.image.ImageDataGenerator(rotation_range=0.2,
                                                           width_shift_range=0.05,
                                                           height_shift_range=0.05,
                                                           shear_range=0.05,
                                                           zoom_range=0.05,
                                                           horizontal_flip=True,
                                                           vertical_flip=True,
                                                           fill_mode='nearest')
    train_datagen = image_data.flow_from_directory(train_dataset_dir,
                                                   class_mode='categorical',
                                                   batch_size = cfg.batch_size,
                                                   target_size=(32,32),
                                                   shuffle=True)
    val_datagen = image_data.flow_from_directory(val_dataset_dir,
                                                 class_mode='categorical',
                                                 batch_size=cfg.batch_size,
                                                 target_size=(32, 32),
                                                 shuffle=True)

    # 初始化相关参数
    interval = 2                                                   # 验证间隔
    train_iter_num = train_datagen.samples // cfg.batch_size       # 训练集1个epoch的迭代次数
    val_iter_num = val_datagen.samples // cfg.batch_size           # 测试集1个epoch的迭代次数

    # 初始化VGG16,并进行测试批量图像
    vgg16 = VGG16(cfg)
    vgg16.train(train_datagen=train_datagen,
                val_datagen=val_datagen,
                train_iter_num=train_iter_num,
                val_iter_num=val_iter_num,
                interval=interval,
                pre_model_path=cfg.pre_model_path)
Ejemplo n.º 6
0
def train(debug: bool = False):
    model = VGG16()
    model.train("3dbox_weights.hdf5", debug)
Ejemplo n.º 7
0
parser = argparse.ArgumentParser(description='DNI')

parser.add_argument('--device', choices=['cpu', 'gpu'], default='cpu')
parser.add_argument('--methods', choices=['batch_norm'], default='batch_norm')

args = parser.parse_args()

if __name__ == '__main__':
    batch_size = 4
    epochs = 1

    train_loader, test_loader, classes = cifar10_load()

    # Set model
    model = VGG16()

    # Set device
    if args.device == 'gpu':
        device = torch.device('cuda' if torch.cuda.is_available() else 'cuda')
        model.to(device)
    else:
        device = 'cpu'

    optimizer = optim.Adam(model.parameters(), lr=0.001, weight_decay=5e-4)
    criterion = nn.CrossEntropyLoss()

    trainer = Trainer(criterion, device, model, optimizer, test_loader,
                      train_loader, epochs)

    trainer.run()
Ejemplo n.º 8
0
def predict(weights, input_path, config, prediction_path):
    model = VGG16()
    model.load_weights(weights)

    data_loader = DataLoader(input_path, config)
    dims_avg, _ = data_loader.get_average_dimension()

    val_imgs = data_loader.valid_images
    val_labels = data_loader.valid_labels

    start_time = time.time()
    for act_img in val_imgs:

        prediction_file = path.join(
            prediction_path,
            path.basename(act_img).replace("jpg", "txt"))
        log.info(f"writing to {prediction_file}")

        act_labels = [x for x in val_labels if x["image"] == act_img]

        with open(prediction_file, "w") as predict:
            for act_label in act_labels:
                img = cv2.imread(act_label["image"])
                img = np.array(img, dtype="float32")

                xmin = int(act_label["xmin"])
                ymin = int(act_label["ymin"])
                xmax = int(act_label["xmax"])
                ymax = int(act_label["ymax"])

                xmin = max(xmin, 0)
                ymin = max(ymin, 0)
                xmax = min(xmax, img.shape[1] - 1)
                ymax = min(ymax, img.shape[0] - 1)

                patch = img[ymin:ymax + 1, xmin:xmax + 1, ]

                patch = cv2.resize(patch, (224, 224))
                patch -= np.array([[[103.939, 116.779, 123.68]]])
                patch = np.expand_dims(patch, 0)

                prediction = model.predict(patch)
                dim = prediction[0][0]

                # Transform regressed angle
                max_anc = np.argmax(prediction[2][0])
                anchors = prediction[1][0][max_anc]

                if anchors[1] > 0:
                    angle_offset = np.arccos(anchors[0])
                else:
                    angle_offset = -np.arccos(anchors[0])

                wedge = 2.0 * np.pi / BIN
                angle_offset = angle_offset + max_anc * wedge
                angle_offset = angle_offset % (2.0 * np.pi)

                angle_offset = angle_offset - np.pi / 2
                if angle_offset > np.pi:
                    angle_offset = angle_offset - (2.0 * np.pi)

                act_label["alpha"] = angle_offset

                # Transform regressed dimension
                act_label["dims"] = dims_avg[act_label["name"]] + dim
                del act_label["P"]

                line = " ".join([str(act_label[item])
                                 for item in act_label]) + "\n"
                predict.write(line)

    end_time = time.time()
    process_time = (end_time - start_time) / len(val_imgs)
    print(process_time)