コード例 #1
0
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])
    img = transform(Image.open(img))
    image = img.unsqueeze(0)
    #image = Variable(image).cuda()
    image = Variable(image)

    cnn = alexnet(embedding_dim=embedding_size)
    rnn = RNN(embedding_dim=embedding_size,
              hidden_dim=hidden_size,
              vocab_size=vocab.index)
    #cnn.cuda()
    #rnn.cuda()

    #cnn_file = str(train_time) + '_iter_' + str(epoch) + '_cnn.pkl'
    #rnn_file = str(train_time) + '_iter_' + str(epoch) + '_rnn.pkl'
    cnn_file = 'alex_iter_' + str(epoch) + '_cnn.pkl'
    rnn_file = 'alex_iter_' + str(epoch) + '_rnn.pkl'
    cnn.load_state_dict(
        torch.load(os.path.join('train_file', cnn_file), map_location='cpu'))
    rnn.load_state_dict(
        torch.load(os.path.join('train_file', rnn_file), map_location='cpu'))

    cnn_out = cnn(image)
    word_id = rnn.search(cnn_out)
    sentence = vocab.get_sentence(word_id)
    print(sentence)

    showimage = Image.open(args.img)
    plt.imshow(np.asarray(showimage))