def __init__(self,device):
        
        self.nz = 100
        self.beta1 = 0.5
        self.real_label = 1
        self.fake_label = 0
        self.L = 100
        self.device = device
        self.iters = 0

        self.netG = Generator(self.nz).to(self.device)
        self.netD = Discriminator().to(self.device)
        self.netTarget = VGG('VGG16').to(self.device)
        self.netTarget.load_state_dict(torch.load('BestClassifierModel.pth',map_location=self.device))

        # fixed_noise -> stores fixed generator seed for inference
        self.fixed_noise = torch.randn(64, self.nz, 1, 1, device=self.device)

        self.netG.apply(self.weights_init)
        self.netD.apply(self.weights_init)

        self.optimizerG = optim.Adam(self.netG.parameters(), lr=2e-4, betas=(self.beta1,0.999))
        self.optimizerD = optim.Adam(self.netD.parameters(), lr=2e-4, betas=(self.beta1,0.999))
        self.criterion = nn.BCELoss()
        self.criterionTarget = nn.CrossEntropyLoss()

        # criterionPerturbation -> norm of the generated noise
        self.criterionPerturbation = nn.MSELoss()
Beispiel #2
0
    def __init__(self, channel=32):
        super(model_VGG, self).__init__()
        self.vgg = VGG()

        self.score = nn.Conv2d(128, 1, 1, 1)

        self.lspm = LSPM(512)

        self.aggregation_4 = aggregation_OWN(512, 512)
        self.aggregation_3 = aggregation_OWN(512, 256)
        self.aggregation_2 = aggregation_OWN(256, 128)
        self.aggregation_1 = aggregation_OWN_Single(128)

        self.out_planes = [512, 256, 128]
        infos = []
        for ii in self.out_planes:
            infos.append(
                nn.Sequential(nn.Conv2d(512, ii, 3, 1, 1, bias=False),
                              nn.ReLU(inplace=True)))
        self.infos = nn.ModuleList(infos)

        self.upsample_2 = nn.Upsample(scale_factor=2,
                                      mode='bilinear',
                                      align_corners=True)
        self.upsample_4 = nn.Upsample(scale_factor=4,
                                      mode='bilinear',
                                      align_corners=True)
        self.upsample_8 = nn.Upsample(scale_factor=8,
                                      mode='bilinear',
                                      align_corners=True)
Beispiel #3
0
    def __init__(self, config):
        self._config = config
        self._kernel_size = 3
        self._cnn = VGG(self._config.nr_feat_maps, self._config.tensor_names,
                        self._config.image_size)
        self._grcu_list = []

        for L, hidden_layer_size in enumerate(self._config.hidden_sizes):
            kernel = self._kernel_size if self._config.input_sizes[L][
                0] > self._kernel_size else self._config.input_sizes[L][0]
            if self._config.stacked:
                if L == 0:
                    self._grcu_list.append(
                        StackedGRCUCell(hidden_layer_size, -1,
                                        self._config.input_sizes[L][0],
                                        self._config.input_sizes[L][1],
                                        self._config.input_sizes[L][2],
                                        kernel))
                else:
                    self._grcu_list.append(
                        StackedGRCUCell(hidden_layer_size,
                                        self._config.hidden_sizes[L - 1],
                                        self._config.input_sizes[L][0],
                                        self._config.input_sizes[L][1],
                                        self._config.input_sizes[L][2],
                                        kernel))
            else:
                self._grcu_list.append(
                    GRCUCell(hidden_layer_size, self._config.input_sizes[L][0],
                             self._config.input_sizes[L][1],
                             self._config.input_sizes[L][2], kernel))
Beispiel #4
0
    def test(self, test_file_path, num_classes, batch_size, model):
        '''
        @description: 构建VGG-Net16网络结构,加载训练好的网络模型,对测试样本进行分类,输出测试准确率
        @params:
            - test_file_path: 测试样本集对应的txt文件所在的路径
            - num_classes: 分类数目
            - batch_size: 测试过程中的每次输入网络中的样本数
            - model: 已经训练好的模型
        @return: None
        '''
        test_file_name = test_file_path + 'test_list.txt'
        vgg = VGG(weight_decay=0.0, keep_prob=1.0, num_classes=num_classes)
        with tf.Graph().as_default(), tf.device('/gpu:0'):

            with tf.name_scope('input'):
                #队列读取训练数据
                num_examples = np.loadtxt(test_file_name,
                                          dtype=np.str).shape[0]
                test_image,test_label = get_batch(test_file_name,self._image_H,\
                                                  self._image_W,batch_size,\
                                                  is_train=False)

                x = tf.placeholder(tf.float32,[None,self._image_H,self._image_W,\
                                               self._image_channels],name='x')

            _, prob = vgg.vgg16(x)
            correct_top_1 = tf.nn.top_k(prob, k=1)
            correct_top_5 = tf.nn.top_k(prob, k=5)

            saver = tf.train.Saver()
            with tf.Session(
                    config=tf.ConfigProto(allow_soft_placement=True,
                                          log_device_placement=True)) as sess:
                saver.restore(sess, model)
                start_time = time.time()
                num_epoch = int(np.ceil(num_examples / batch_size))
                num_examples = num_epoch * batch_size
                index_top_1 = np.zeros(num_examples)
                index_top_5 = np.zeros((num_examples, 5))
                label = np.zeros(num_examples)
                for i in range(num_epoch):
                    image,label[i*batch_size:(i+1)*batch_size] = \
                    sess.run([test_image,test_label])
                    index_top_1[i*batch_size:(i+1)*batch_size],\
                    index_top_5[i*batch_size:(i+1)*batch_size] = \
                    sess.run(correct_top_1[1],correct_top_5,feed_dict={x:image})
                duration = time.time() - start_time

                top_1 = 0
                top_5 = 0
                for i in range(num_examples):
                    top_1 += label[i] in index_top_1[i]
                    top_5 += label[i] in index_top_5[i]
                top_1 = top_1 / num_examples
                top_5 = top_5 / num_examples

                print('top_1 accuracy: %.3f%%, top_5 accuracy: %.3f%%\t%.3fsec)'%\
                     (top_1,top_5,duration))
Beispiel #5
0
class model_VGG(nn.Module):
    def __init__(self, channel=32):
        super(model_VGG, self).__init__()
        self.vgg = VGG()

        self.score = nn.Conv2d(128, 1, 1, 1)

        self.lspm = LSPM(512)

        self.aggregation_4 = aggregation_OWN(512, 512)
        self.aggregation_3 = aggregation_OWN(512, 256)
        self.aggregation_2 = aggregation_OWN(256, 128)
        self.aggregation_1 = aggregation_OWN_Single(128)

        self.out_planes = [512, 256, 128]
        infos = []
        for ii in self.out_planes:
            infos.append(
                nn.Sequential(nn.Conv2d(512, ii, 3, 1, 1, bias=False),
                              nn.ReLU(inplace=True)))
        self.infos = nn.ModuleList(infos)

        self.upsample_2 = nn.Upsample(scale_factor=2,
                                      mode='bilinear',
                                      align_corners=True)
        self.upsample_4 = nn.Upsample(scale_factor=4,
                                      mode='bilinear',
                                      align_corners=True)
        self.upsample_8 = nn.Upsample(scale_factor=8,
                                      mode='bilinear',
                                      align_corners=True)

    def forward(self, x):

        x1 = self.vgg.conv1(x)
        x2 = self.vgg.conv2(x1)
        x3 = self.vgg.conv3(x2)
        x4 = self.vgg.conv4(x3)
        x5 = self.vgg.conv5(x4)

        lspm = self.lspm(x5)
        GG = []
        GG.append(self.infos[0](self.upsample_2(lspm)))
        GG.append(self.infos[1](self.upsample_4(lspm)))
        GG.append(self.infos[2](self.upsample_8(lspm)))

        merge = self.aggregation_4(x4, x5, GG[0])
        merge = self.aggregation_3(x3, merge, GG[1])
        merge = self.aggregation_2(x2, merge, GG[2])
        merge = self.aggregation_1(merge)
        merge = self.score(merge)
        result = F.interpolate(merge,
                               x1.size()[2],
                               mode='bilinear',
                               align_corners=True)

        return result
Beispiel #6
0
def test_vgg11(batch_size, batch_norm, num_classes, model_name):
    input = torch.randn(batch_size, 3, 32, 32)
    model = VGG(
        config={
            "name": model_name,
            "params": VGG_CONFIG[model_name],
        },
        batch_norm=batch_norm,
        num_classes=num_classes
    )
    output = model(input)
    assert model.name() == model_name
    assert list(output.size()) == [batch_size, num_classes]
Beispiel #7
0
def test(test_model_dir,
         batch_size,
         mean=(0.5, 0.5, 0.5),
         std=(0.5, 0.5, 0.5),
         model=None):
    transform = transforms.Compose([
        transforms.Resize(32),
        transforms.ToTensor(),
        transforms.Normalize(mean, std)
    ])
    test_dataset = torchvision.datasets.CIFAR100("../../datasets/",
                                                 download=True,
                                                 train=False,
                                                 transform=transform)
    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=batch_size,
                                              shuffle=True)

    if model is None:
        state_dict = torch.load(test_model_dir)
        model = VGG(state_dict['model_type'], True).cuda()
        model.load_state_dict(state_dict["model_state_dict"])

    classes = []
    accuracy = 0.0
    with torch.no_grad():
        tot = 0
        correct = 0

        for idx, (x, y) in enumerate(test_loader):
            x = x.cuda()
            y = y.cuda()

            out = model(x)
            _, predicted = torch.max(out.data, 1)

            for j in range(len(predicted)):
                tot = tot + 1
                if predicted[j] == y.cpu()[j]:
                    correct = correct + 1

            print("Test Percent Finished: {}%.".format(idx * 100 /
                                                       len(test_loader)))

        print(
            "{} th Picture Predicted: Correct Predict: {}. Correct Percentage: {:.2f}%"
            .format(tot, correct, correct * 100 / tot))
        accuracy = correct / tot * 100
    return accuracy
Beispiel #8
0
def style_transfer(content_image_path, style_image_path, mixed_image_path,
                   content_weight, style_weight, variation_weight, pooling,
                   learning_rate, beta1, beta2, epsilon, max_iteration,
                   check_point):
    # set the time point
    time_start = time.time()

    # load image
    content_image = load_image(content_image_path)
    style_image = load_image(style_image_path, shape=content_image.shape[:2])

    # initialize object
    vgg = VGG(VGG_MAT_PATH, pooling)
    nn = NeuralNetwork(content_image, style_image, vgg, content_weight,
                       style_weight, variation_weight)

    # train the model
    for i, mixed_image in nn.train_model(learning_rate, beta1, beta2, epsilon,
                                         max_iteration, check_point):
        save_image(mixed_image_path + '\\v1_{}.jpeg'.format(i + 1),
                   mixed_image)

    # print time
    time_end = time.time()
    print('Time elapsed: {} seconds'.format(round(time_end - time_start)))

    return
def get_model(model_name, pho_size=299, num_classes=110):
    if model_name == "vgg16":
        model = VGG(num_classes=num_classes, pho_size=299)
    elif model_name == "resnet101":
        model = resnet101(num_classes=num_classes)
    elif model_name == "densenet":
        model = DenseNet(growth_rate=12,
                         block_config=[(100 - 4) // 6 for _ in range(3)],
                         num_classes=num_classes,
                         small_inputs=False,
                         efficient=True,
                         pho_size=pho_size)
    elif model_name == "InceptionResNetV2":
        model = InceptionResNetV2(num_classes=num_classes)
    elif model_name == "InceptionV4":
        model = InceptionV4(num_classes=num_classes)
    elif model_name == "Inception3":
        model = Inception3(num_classes=num_classes)
    elif model_name == "denoise":
        model = get_denoise()
    elif model_name == "Mymodel":
        model = Mymodel()
    elif model_name == 'Comdefend':
        model = ComDefend()
    elif model_name == 'Rectifi':
        model = Rectifi()
    return model
Beispiel #10
0
def main():

    args = parser()

    if args is None:
        exit()

    if args.verbose:
        print('Arguments parsed....')

    # Model instance
    vgg = VGG(args.model_path, args.pool_type, args.lalpha)

    if args.verbose:
        print('Model created....')

    # Content and Style Images
    content_image = load_image(os.path.join(args.content_path,
                                            args.content_image),
                               max_size=args.max_size)
    style_images = [
        load_image(os.path.join(args.style_path, image),
                   shape=(content_image.shape[1], content_image.shape[0]))
        for image in args.style_images
    ]

    if args.verbose:
        print('Content and style images loaded....')

    if args.initial_type == 'content':
        init_gen_image = content_image
    elif args.initial_type == 'style':
        init_gen_image = style_images[0]
    elif args.initial_type == 'random':
        init_gen_image = get_content_image(content_image, args.noise_ratio,
                                           args.seed)

    if args.verbose:
        print('Generated image initialized....')

    # Stylize instance
    stylize = Stylize(vgg, content_image, style_images, init_gen_image, args)

    if args.verbose:
        print('Style-model created....')
        print('Generating image....')

    # Transfer style
    gen_image = stylize.transfer_style()

    if args.verbose:
        print('Image generated....')

    # Saving the image to destination path
    save_image(args.out_filepath, gen_image)

    if args.verbose:
        print('Generated image saved....')
        print('Completed!!!! :)')
Beispiel #11
0
def get_model(device):
    model_path = config.PATH_PTHS + config.MODEL
    model = VGG(pretrained=False).to(device)
    model.load_state_dict(
        torch.load(model_path,
                   map_location=None if torch.cuda.is_available() else "cpu"))
    model.eval()
    return model
Beispiel #12
0
def get_model(path='pretrained/ckpt.t7'):
    #model = nn.DataParallel(VGG('VGG16'))
    model = VGG('VGG16')
    #if True:
    #    print(model._modules['features']._modules)
    #checkpoint=torch.load(path, map_location='cpu')
    #checkpoint=torch.load(path, map_location=lambda storage, loc:storage)
    #model was trained on GPU
    state_dict = torch.load(path)['net']
    if False:
        print(state_dict.keys())
    if False:
        print(list(model._modules.keys()))
        if use_gpu:
            print(model._modules['module']._modules['features']._modules)
        else:
            print(model._modules['features']._modules)

    if use_gpu:
        model = nn.DataParallel(VGG('VGG16'))
    else:
        model = VGG('VGG16')
        new_dict = OrderedDict()
        for key, val in state_dict.items():
            key = key[7:]  #since 'module.' has len 7
            new_dict[key] = val.to('cpu')
        state_dict = new_dict
    if False:
        print(state_dict.keys())
    model.load_state_dict(state_dict)
    return model
Beispiel #13
0
def main():
    model = VGG(depth=16, init_weights=True, cfg=None)
    # model = VGG_shaokai("vgg16")
    # model = ConvNet()
    # model = ResNet18()
    # model = torch.nn.DataParallel(model)
    model.load_state_dict(torch.load("./model_pruned/2019-04-09 11:14:52.016169/column-filter-fivelabels-masked_retrain/cifar10_vgg16_retrained_acc_93.960_4rhos_config_vgg16_v2.yaml.pt"))
    model.cuda()

    criterion = F.cross_entropy
#    criterion = CrossEntropyLossMaybeSmooth(smooth_eps=0).cuda()
    validate(test_loader, criterion, model)
    # test(model, criterion, test_loader)
    print("\n------------------------------\n")


    print('here')
    for name, weight in model.named_parameters():
        if (len(weight.size()) == 4 and "shortcut" not in name):
            print(name, weight.size())


    print('here now')
    test_column_sparsity(model)
    # test_chanel_sparsity(model)
    test_filter_sparsity(model)
Beispiel #14
0
def main(args):
    # 数据加载
    (x_train, y_train), (x_test, y_test) = load_cifar(args.cifar_root)

    # 随机选择训练样本
    train_num = x_train.shape[0]

    def next_batch(batch_size):
        idx = np.random.choice(train_num, batch_size)
        return x_train[idx], y_train[idx]

    # 网络
    vgg = VGG(image_size=32, name='vgg11')
    opt = RmsProp(vgg.weights, lr=args.lr, decay=1e-3)

    # 加载权重
    if args.checkpoint:
        weights = load_weights(args.checkpoint)
        vgg.load_weights(weights)
        print("load weights done")

    # 评估
    if args.eval_only:
        indices = np.random.choice(len(x_test), args.eval_num, replace=False)
        print('{} start evaluate'.format(
            time.asctime(time.localtime(time.time()))))
        acc = get_accuracy(vgg, x_test[indices], ys=y_test[indices])
        print('{} acc on test dataset is :{:.3f}'.format(
            time.asctime(time.localtime(time.time())), acc))
        return

    # 训练
    num_steps = args.steps
    for step in range(num_steps):
        x, y_true = next_batch(args.batch_size)
        # 前向传播
        y_predict = vgg.forward(x.astype(np.float))
        # print('y_pred: min{},max{},mean:{}'.format(np.min(y_predict, axis=-1),
        #                                            np.max(y_predict, axis=-1),
        #                                            np.mean(y_predict, axis=-1)))
        # print('y_pred: {}'.format(y_predict))
        acc = np.mean(
            np.argmax(y_predict, axis=1) == np.argmax(y_true, axis=1))
        # 计算loss
        loss, gradient = cross_entropy_loss(y_predict, y_true)

        # 反向传播
        vgg.backward(gradient)
        # 更新梯度
        opt.iterate(vgg)

        # 打印信息
        print('{} step:{},loss:{:.4f},acc:{:.4f}'.format(
            time.asctime(time.localtime(time.time())), step, loss, acc))

        # 保存权重
        if step % 100 == 0:
            save_weights(
                os.path.join(args.save_dir, 'weights-{:03d}.pkl'.format(step)),
                vgg.weights)
def get_vgg_net(model_folder, out_keys=['r11', 'r21', 'r31', 'r41', 'r51']):

    vgg_net = VGG(pool='avg', out_keys=out_keys)
    vgg_net.load_state_dict(torch.load(model_folder + 'vgg_conv.pth'))
    vgg_net.cuda()
    for param in vgg_net.parameters():
        param.requires_grad = False
    return vgg_net
def feature_visual(image_path,
                   feature_image_path,
                   pooling,
                   learning_rate,
                   beta1,
                   beta2,
                   epsilon,
                   max_iteration,
                   check_point,
                   init_image='random',
                   feature_image_name='feat_vis',
                   type='filter'):
    # set the time point
    time_start = time.time()

    # load image
    image = load_image(image_path)

    # initialize object
    vgg = VGG(VGG_MAT_PATH, pooling)
    nn = NeuralNetwork(image, vgg, type=type)

    # train the model
    for feature_image, losses in nn.train_model(learning_rate, beta1, beta2,
                                                epsilon, max_iteration,
                                                check_point, init_image):
        if len(losses) > 200:
            plt.plot(
                np.arange(start=len(losses) - 150, stop=len(losses), step=1),
                losses[len(losses) - 150:len(losses)])
            plt.savefig(feature_image_path + r'/' + feature_image_name +
                        '_loss.jpeg')
            plt.close()
        elif losses[len(losses) - 1] == 0:
            plt.plot(losses)
            plt.savefig(feature_image_path + r'/' + feature_image_name +
                        '_loss.jpeg')
            plt.close()
            save_image(
                feature_image_path + r'/' + feature_image_name + '_image.jpeg',
                feature_image)
            break
        else:
            plt.plot(losses)
            plt.savefig(feature_image_path + r'/' + feature_image_name +
                        '_loss.jpeg')
            plt.close()
        save_image(
            feature_image_path + r'/' + feature_image_name + '_image.jpeg',
            feature_image)

    # print time
    time_end = time.time()
    print('Time elapsed: {} seconds'.format(round(time_end - time_start)))

    return
Beispiel #17
0
def test(path):
    (x_train, y_train), (x_test, y_test) = load_cifar(path)
    print(x_train[0][0])
    print(y_train[0])
    vgg = VGG(name='vgg11')
    import utils
    utils.save_weights('./w.pkl', vgg.weights)
    w = utils.load_weights('./w.pkl')
    print(type(w))
    print(w.keys())
 def __init__(self, in_size, num_classes, visualize=False):
     super(FasterRCNN, self).__init__()
     self.in_size = in_size
     self.num_classes = num_classes
     self.visualize = visualize
     self.device = torch.device(
         'cuda:0' if torch.cuda.is_available() else 'cpu')
     # self.backbone = self.build_backbone()
     self.backbone = VGG('A', self.num_classes).to(self.device)
     self.rpn = RPN(512, 512, self.in_size, 9).to(self.device)
Beispiel #19
0
def getNetwork(args):
    if args.net_type == 'resnet':
        net = ResNet18(100)
        file_name = 'resnet18'
    elif args.net_type == 'vgg':
        net = VGG('VGG16', 100)
        file_name = 'vgg'
    else:
        assert False
    file_name += '_' + str(args.seed) + '_' + args.exp_name
    return net, file_name
Beispiel #20
0
def get_features(image):
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])

    model = VGG().to(device)
    image = transform(image).to(device)
    image = image.view((1, *image.size()))
    return model(image)
def main(_):
    pp.pprint(flags.FLAGS.__flags)

    #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
    run_config = tf.ConfigProto()
    #run_config.gpu_options.allow_growth=True

    # Data initialisation
    data_path = os.path.join(FLAGS.data_dir, FLAGS.dataset,
                             FLAGS.input_fname_pattern)
    data = ImageDataset(regex_file=data_path,
                        input_height=FLAGS.input_height,
                        input_width=FLAGS.input_width,
                        as_line=False)
    y_pred = label_processing(data.getData())
    labels = Label(y_pred)
    dataset = LabeledDataset(data, labels)

    with tf.Session() as sess:

        #net = NN(FLAGS.input_height*FLAGS.input_width*3, 2, hidden_layers =  [60,10, 30],
        #        learning_rate = FLAGS.learning_rate, build = True, name = 'FCNeuralNet')
        net = VGG([FLAGS.input_height, FLAGS.input_width, 3],
                  2,
                  learning_rate=FLAGS.learning_rate,
                  build=True,
                  name='VGGNeuralNet')
        model = Classifier1D(sess, [net],
                             dataset,
                             checkpoint_dir=FLAGS.checkpoint_dir,
                             model_name=FLAGS.model_name)

        old, step = model.load(FLAGS.checkpoint_dir)
        print("(*) Already train : {} {}".format(
            old, ("--> step " + str(step) if old else "")))

        if FLAGS.train:
            model.train(FLAGS)
        else:
            if not model.load(FLAGS.checkpoint_dir)[0]:
                raise Exception("(!) Train a model first, then run test mode")
            else:
                model.test(FLAGS)
def main(args):
    if args.model == 'lenet5':
        from net import Net
        net = Net()
    elif args.model == 'cnn':
        from cnn_net import Net
        net = Net()
    elif args.model == 'gap':
        from net_gap import Net
        net = Net()
    elif args.model == 'vgg':
        from vgg import VGG
        net = VGG()

    # device = torch.device("cuda:0" if torch.cuda.is_available() else 'cpu')
    # print(device)
    net.to(device)

    trainloader, testloader = load_data_cifar()
    train(net, trainloader, testloader, args.num_epoch)

    # PATH = './cifar_net.pth'
    torch.save(net.state_dict(), './cifar_net_{}.pth'.format(args.model))

    class_correct = list(0. for i in range(10))
    class_total = list(0. for i in range(10))
    with torch.no_grad():
        for data in testloader:
            images, labels = data
            inputs = inputs.to(device)
            labels = labels.to(device)
            outputs = net(images)
            _, predicted = torch.max(outputs, 1)
            c = (predicted == labels).squeeze()
            for i in range(4):
                label = labels[i]
                class_correct[label] += c[i].item()
                class_total[label] += 1

    for i in range(10):
        print('Accuracy of %5s : %2d %%' % (
            classes[i], 100 * class_correct[i] / class_total[i]))
    def build_backbone(self):
        in_h, in_w = self.in_size[0], self.in_size[1]
        model = VGG('A', self.num_classes).to(self.device)
        # model = models.vgg16(pretrained=True).to(self.device)
        # features = list(model.features)
        #
        # dummy_img = torch.zeros((1, 3, in_h, in_w)).float()
        # req_features = []
        # dummy = dummy_img.clone().to(self.device)
        #
        # for feature in features:
        #     dummy = feature(dummy)
        #
        #     if dummy.size()[2] < 800 // 16:
        #         break
        #     req_features.append(feature)
        #     out_dim = dummy.size()[1]

        # return nn.Sequential(*req_features)
        return model
def run():
    classes = ('Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise',
               'Neutral')
    crop_size = 44
    trained_model = torch.load("C:/Users/Admin/Downloads/model_state.pth.tar")
    model = VGG("VGG19")
    model.load_state_dict(trained_model["model_weights"])
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model.to(device)
    model.eval()

    st.title("Facial expression recognition")
    img_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])

    if img_file is None:
        st.write('** Please upload an image **')
    original_image = Image.open(img_file, mode='r')
    st.image(original_image, use_column_width=True)
    model = 1
    if st.button('Predict'):
        predict_image = detect(model, original_image)
        image = Image.fromarray(cv2.cvtColor(predict_image, cv2.COLOR_BGR2RGB))
        st.image(image, use_column_width=True)
Beispiel #25
0
def main():
    if FLAGS.cnn_type == 'inception':
        cnn = Inception()
    else:
        cnn = VGG(FLAGS.nr_feat_maps, FLAGS.tensor_names, FLAGS.image_size)
        # cnn.printTensors()

    dict_test = {}
    dict_test["video_path"] = []
    dict_test["label"] = []
    with open(FLAGS.test_list) as f:
        for line in f:
            video_path, label = line.split()
            dict_test["video_path"].append(video_path)
            dict_test["label"].append(label)

    test_data = pd.DataFrame(data=dict_test, columns=['video_path', 'label'])

    if not os.path.exists(FLAGS.test_data_file):
        test_data.to_csv(FLAGS.test_data_file, sep=',')

    dict = {}
    dict["video_path"] = []
    dict["label"] = []
    dict["feat_path"] = []
    with open(FLAGS.train_list) as f:
        for line in f:
            video_path, label = line.split()
            dict["video_path"].append(video_path)
            dict["label"].append(label)
            dict["feat_path"].append(video_path.split("/")[-1] + ".pkl")

    train_data = pd.DataFrame(data=dict,
                              columns=['video_path', 'label', "feat_path"])

    if not os.path.exists(FLAGS.train_data_file):
        train_data.to_csv(FLAGS.train_data_file, sep=',')

    train_data.apply(lambda row: process_record(cnn, row), axis=1)
Beispiel #26
0
from vgg import VGG
import os

model = VGG()
print(model.summary())
weights = './model/emotion_best_weights.h5'
if os.path.exists(weights):
    print("Loading weight")
    model.load_weights(weights)
    print("Saving model")
    model.save("./model/emotion_recognition.h5")
Beispiel #27
0
    styleImg = styleImg.unsqueeze(0)
    contentImg = contentImg.unsqueeze(0)
    styleImg, contentImg, content_iq = util.luminance_transfer(
        styleImg.numpy(), contentImg.numpy())
    styleImg = Variable(torch.from_numpy(styleImg))
    contentImg = Variable(torch.from_numpy(contentImg))
else:
    styleImg = load_image(opt.style_image)  # 1x3x512x512
    contentImg = load_image(opt.content_image)  # 1x3x512x512

if (opt.cuda):
    styleImg = styleImg.cuda()
    contentImg = contentImg.cuda()

###############   MODEL   ####################
vgg = VGG()
vgg.load_state_dict(torch.load(opt.vgg_dir))
for param in vgg.parameters():
    param.requires_grad = False
if (opt.cuda):
    vgg.cuda()


###########   LOSS & OPTIMIZER   ##########
class GramMatrix(nn.Module):
    def forward(self, input):
        b, c, h, w = input.size()
        f = input.view(b, c, h * w)  # bxcx(hxw)
        # torch.bmm(batch1, batch2, out=None)   #
        # batch1: bxmxp, batch2: bxpxn -> bxmxn #
        G = torch.bmm(f, f.transpose(
Beispiel #28
0
    img = img.clamp_(0, 1)
    tutils.save_image(img, '%s/transfer2.png' % ("./images"), normalize=True)
    return


style_img = "./QiBashi.jpg"
content_img = "./2.jpg"
styleImg = load_img(style_img)
contentImg = load_img(content_img)

#for running on cuda
styleImg = styleImg.cuda()
contentImg = contentImg.cuda()

vgg_directory = "./vgg_conv.pth"  #path to pretrained vgg vgg_directory
vgg = VGG()
#print(vgg.state_dict())
vgg.load_state_dict(torch.load(vgg_directory))
for param in vgg.parameters():
    param.requires_grad = False

vgg.cuda()  # Putting model on cuda


class GramMatrix(nn.Module):
    def forward(self, input):
        b, c, h, w = input.size()
        f = input.view(b, c, h * w)  #bxcx(hxw)
        # torch.bmm(batch1, batch2, out=None)
        # batch1 : bxmxp, batch2 : bxpxn -> bxmxn
        G = torch.bmm(f, f.transpose(
def train():
    transform_train = transforms.Compose([
        transforms.RandomHorizontalFlip(),
        transforms.RandomCrop(size=32),
        transforms.ToTensor(),
        transforms.Normalize((0.4914, 0.4822, 0.4465),
                             (0.2023, 0.1994, 0.2010)),
    ])

    transform_test = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.4914, 0.4822, 0.4465),
                             (0.2023, 0.1994, 0.2010)),
    ])

    trainset = torchvision.datasets.CIFAR10(root='./data',
                                            train=True,
                                            download=False,
                                            transform=transform_train)
    trainloader = torch.utils.data.DataLoader(trainset,
                                              batch_size=args.batch_size,
                                              shuffle=True,
                                              num_workers=2)

    testset = torchvision.datasets.CIFAR10(root='./data',
                                           train=False,
                                           download=False,
                                           transform=transform_test)
    testloader = torch.utils.data.DataLoader(testset,
                                             batch_size=args.batch_size,
                                             shuffle=True,
                                             num_workers=2)

    classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
               'ship', 'truck')

    model = VGG(vars(args))
    optimizer = torch.optim.SGD(model.parameters(),
                                lr=args.lrate,
                                momentum=0.9,
                                weight_decay=5e-4)

    if args.use_cuda:
        model = model.cuda()

    if args.eval:
        model.load_state_dict(torch.load(args.model_dir))
        model.eval()
        accuracy = model.evaluate(testloader)
        exit()

    total_size = len(trainloader)
    lrate = args.lrate
    best_score = 0.0
    scores = []
    for epoch in range(1, args.epochs + 1):
        model.train()
        for i, (image, label) in enumerate(trainloader):

            loss = model(image, label)
            model.zero_grad()
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            if i % 100 == 0:
                print('Epoch = %d, step = %d / %d, loss = %.5f lrate = %.5f' %
                      (epoch, i, total_size, loss, lrate))

        model.eval()
        accuracy = model.evaluate(testloader)
        scores.append(accuracy)

        with open(args.model_dir + "_scores.pkl", "wb") as f:
            pkl.dump(scores, f)

        if best_score < accuracy:
            best_score = accuracy
            print('saving %s ...' % args.model_dir)
            torch.save(model.state_dict(), args.model_dir)

        if epoch % args.decay_period == 0:
            lrate *= args.decay
            for param_group in optimizer.param_groups:
                param_group['lr'] = lrate
Beispiel #30
0
def main():
    date = str(datetime.datetime.now())
    
    torch.cuda.set_device(0)
    train_loader, test_loader = getData(args.no_Of_Labels)
        
    if args.depth == 16:
        model = VGG(depth=16, init_weights=True, cfg=None)
    if args.depth == 19:
        model = VGG(depth=19, init_weights=True, cfg=None)
                
    admm = False
    masked_retrain = False

    if args.admm:
        admm = True
    if args.masked_retrain:
        masked_retrain = True
    else:
        print("no sparsity type specified")
        return


    config = args.config_file
    prune_ratios = []
    if not isinstance(config, str):
        raise Exception("filename must be a str")
    with open(config, "r") as stream:
        try:
            raw_dict = yaml.load(stream)
            prune_ratios = raw_dict['prune_ratios']  #this should be a list of dictionaries
            
        except yaml.YAMLError as exc:
            print(exc)

    masks = []
    base_model_path = args.base_model_path

    pruned_path = "./model_pruned/"+date+"/"
    if not os.path.exists(pruned_path):
        os.makedirs(pruned_path)
    copyfile(config, pruned_path+config)
    
    for i in range(len(prune_ratios)):
        prune_ratio = prune_ratios[i]
        print(prune_ratio)
        sparsity_type = prune_ratio['prune_ratio_'+str(i+1)]['type']
        prune_values = prune_ratio['prune_ratio_'+str(i+1)]['values']

        pruned_path = "./model_pruned/"+date+"/"
        for j in range(i+1):
            pruned_path += prune_ratios[j]['prune_ratio_'+str(j+1)]['type']
            pruned_path += '-'
        if args.no_Of_Labels == 2:
            pruned_path += 'twolabels-'
        if args.no_Of_Labels == 5:
            pruned_path += 'fivelabels-'
        if args.no_Of_Labels == 10:
            pruned_path += 'tenlabels-'

        admm_path = pruned_path +'admm'
        masked_path = pruned_path + 'masked_retrain'

        if not os.path.exists(admm_path):
                os.makedirs(admm_path)
        if not os.path.exists(masked_path):
                os.makedirs(masked_path)

        
        if args.admm and args.masked_retrain:
            saved_model = do_admmtrain(args,model,train_loader,test_loader,sparsity_type,prune_values, masks,base_model_path,admm_path)
            masked_model, mask = do_masked_retrain(args,model,train_loader,test_loader,sparsity_type,prune_values,masks,saved_model,masked_path)
            #masks.append(mask)
            base_model_path = masked_model
        elif args.admm:
            do_admmtrain(args,model,train_loader,test_loader,sparsity_type,prune_values,masks, base_model, admm_path)
        elif args.masked_retrain:
            do_masked_retrain(args,model, trained_loader, test_loader, sparsity_type, prune_values ,masks,saved_model,masked_path)
        else:
            print('error')
Beispiel #31
0
    contentImg = transform(util.open_and_resize_image(opt.content_image,256)) # 1x3x512x512
    styleImg = styleImg.unsqueeze(0)
    contentImg = contentImg.unsqueeze(0)
    styleImg,contentImg,content_iq = util.luminance_transfer(styleImg.numpy(),contentImg.numpy())
    styleImg = Variable(torch.from_numpy(styleImg))
    contentImg = Variable(torch.from_numpy(contentImg))
else:
    styleImg = load_image(opt.style_image) # 1x3x512x512
    contentImg = load_image(opt.content_image) # 1x3x512x512

if(opt.cuda):
    styleImg = styleImg.cuda()
    contentImg = contentImg.cuda()

###############   MODEL   ####################
vgg = VGG()
vgg.load_state_dict(torch.load(opt.vgg_dir))
for param in vgg.parameters():
    param.requires_grad = False
if(opt.cuda):
    vgg.cuda()
###########   LOSS & OPTIMIZER   ##########
class GramMatrix(nn.Module):
    def forward(self,input):
        b, c, h, w = input.size()
        f = input.view(b,c,h*w) # bxcx(hxw)
        # torch.bmm(batch1, batch2, out=None)   #
        # batch1: bxmxp, batch2: bxpxn -> bxmxn #
        G = torch.bmm(f,f.transpose(1,2)) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
        return G.div_(h*w)