return features

    sF_loss = get_features(styleV, resnet)

    cF_loss = get_features(contentV, resnet)
    tF = get_features(transfer, resnet)
    loss, styleLoss, contentLoss = criterion(tF, sF_loss, cF_loss)

    # backward & optimization
    loss.backward()
    optimizer.step()
    print(
        'Iteration: [%d/%d] Loss: %.4f contentLoss: %.4f styleLoss: %.4f Learng Rate is %.6f'
        % (opt.niter, iteration, loss, contentLoss, styleLoss,
           optimizer.param_groups[0]['lr']))

    adjust_learning_rate(optimizer, iteration)

    if ((iteration) % opt.log_interval == 0):
        transfer = transfer.clamp(0, 1)
        concat = torch.cat((content, style, transfer.cpu()), dim=0)
        vutils.save_image(concat,
                          '%s/%d.png' % (opt.outf, iteration),
                          normalize=True,
                          scale_each=True,
                          nrow=opt.batchSize)

    if (iteration > 0 and (iteration) % opt.save_interval == 0):
        torch.save(matrix.state_dict(),
                   '%s/%s_resnet_softmax_imagenet.pth' % (opt.outf, opt.layer))
    else:
        feature, transmatrix = matrix(cF, sF)
    transfer = dec(feature)

    sF_loss = vgg5(styleV)
    cF_loss = vgg5(contentV)
    tF = vgg5(transfer)
    loss, styleLoss, contentLoss = criterion(tF, sF_loss, cF_loss)

    # backward & optimization
    loss.backward()
    optimizer.step()
    print(
        'Iteration: [%d/%d] Loss: %.4f contentLoss: %.4f styleLoss: %.4f Learng Rate is %.6f'
        % (opt.niter, iteration, loss, contentLoss, styleLoss,
           optimizer.param_groups[0]['lr']))

    adjust_learning_rate(optimizer, iteration)

    if ((iteration) % opt.log_interval == 0):
        transfer = transfer.clamp(0, 1)
        concat = torch.cat((content, style, transfer.data.cpu()), dim=0)
        vutils.save_image(concat,
                          '%s/%d.png' % (opt.outf, iteration),
                          normalize=True,
                          scale_each=True,
                          nrow=opt.batchSize)

    if (iteration > 0 and (iteration) % opt.save_interval == 0):
        torch.save(matrix.state_dict(), '%s/%s.pth' % (opt.outf, opt.layer))
Exemple #3
0
                if str(name) in layers3:
                    features[layers3[str(name)]] = x
            for name, layer in enumerate(model.layer4):
                x = layer(x)
                if str(name) in layers4:
                    features[layers4[str(name)]] = x

        return features

    sF_loss = get_features(styleV, resnet)

    cF_loss = get_features(contentV, resnet)
    tF = get_features(transfer, resnet)
    loss,styleLoss,contentLoss = criterion(tF,sF_loss,cF_loss)

    # backward & optimization
    loss.backward()
    optimizer.step()
    print('Iteration: [%d/%d] Loss: %.4f contentLoss: %.4f styleLoss: %.4f Learng Rate is %.6f'%
         (opt.niter,iteration,loss,contentLoss,styleLoss,optimizer.param_groups[0]['lr']))

    adjust_learning_rate(optimizer,iteration)

    if((iteration) % opt.log_interval == 0):
        transfer = transfer.clamp(0,1)
        concat = torch.cat((content,style,transfer.cpu()),dim=0)
        vutils.save_image(concat,'%s/%d.png'%(opt.outf,iteration),normalize=True,scale_each=True,nrow=opt.batchSize)

    if(iteration > 0 and (iteration) % opt.save_interval == 0):
        torch.save(matrix.state_dict(), '%s/%s_resnet_random.pth' % (opt.outf,opt.layer))