コード例 #1
0
ファイル: main.py プロジェクト: BulletSentence/X-RayVision
def show_images(images, labels, preds):
    plt.figure(figsize=(8, 4))
    for i, image in enumerate(images):
        plt.subplot(1, 6, i + 1, xticks=[], yticks=[])
        image = image.numpy().transpose((1, 2, 0))
        mean = np.array([0.485, 0.456, 0.406])
        std = np.array([0.229, 0.224, 0.225])
        image = image * std + mean
        image = np.clip(image, 0., 1.)
        plt.imshow(image)
        col = 'green'
        if preds[i] != labels[i]:
            col = 'red'

        plt.xlabel(f'{class_names[int(labels[i].numpy())]}')
        plt.ylabel(f'{class_names[int(preds[i].numpy())]}', color=col)
    plt.tight_layout()
    plt.show()
コード例 #2
0
    if torch.cuda.is_available():
        model.cuda()
    for epoch in range(epoches):  #循环epoch,设置学习率
        if epoch in [epoches * 0.25, epoches * 0.5]:
            for param_group in optimizier.param_groups:
                param_group['lr'] *= 0.1
        for img, _ in train_data:
            img = img.view(img.size(0), -1)
            img = Variable(img.cuda())
            #forward
            _, output = model(img)  #把数据放入模型
            loss = criterion(output, img)  #损失
            #backward
            optimizier.zero_grad()
            loss.backward()
            optimizier.step()
        print('epoch=', epoch, loss.data.float())
        for param_group in optimizier.param_groups:
            print(param_group['lr'])
        if (epoch + 1) % 5 == 0:
            pic = to_img(output.cpu().data)
            if not os.path.exists('./simple_autoencoder'):  #判断该路径是否存在
                os.mkdir(pic, './simple_autoencoder/image_{}.png'.format(
                    epoch + 1))  #如果不存在则将处理后的数据放入该路径
    code = Variable(torch.FloatTensor([1.19, -3.36, 2.06]).cuda())
    decode = model.decoder(code)  #解码
    decode_img = to_img(decode).squeeze()
    decode_img = decode_img.data.cpu().numpy() * 255
    plt.imshow(decode_img.astype('uint8'), cmap='gray')
    plt.show()
コード例 #3
0
sys.path.append('.')  # noqa: E402
from src.recurrent_attention_network_paper.StanfordDogLoader import StanfordDogLoader


if __name__ == '__main__':
    # trainset=CUB200_loader('external/CUB_200_2011',split='train')
    trainset = StanfordDogLoader('external/StanfordDog', split='train')
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, collate_fn=trainset.CUB_collate,
                                              num_workers=4)

    first,labels= next(iter(trainloader))
    print(first[0].mean())
    print(first[0].std())

    firstImage= trainset.tensor_to_img(first[0])
    plt.imshow(firstImage)
    plt.savefig('test.jpg')

    # num = len(trainset)
    #
    # imageNum=10
    #
    # total_sum = 0
    # dataIter= iter(trainloader)
    # for i in range(0,imageNum):
    #     curr = next(dataIter)
    #     total_sum += curr[0].mean()
    #
    # mean = total_sum / imageNum
    # print(mean)
コード例 #4
0
epochs = 15
batch_size = 128
history = model.fit(x_train, x_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, x_test))

#-----------------------------------------------------查看解码效果--------------------------------------------
decoded_imgs = model.predict(x_test)
n = 10
plt.figure(figsize=(20, 6))
for i in range(n):
    # 原图
    ax = plt.subplot(3, n, i+1)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)


    # 解码效果图
    ax = plt.subplot(3, n, i+n+1)
    plt.imshow(decoded_imgs[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

plt.show()
#----------------------------------------------------训练过程可视化---------------------------------------------
print(history.history.keys())
コード例 #5
0
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.summary()

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
hist = model.fit(x_train,
                 y_train,
                 batch_size=batch_size,
                 epochs=epochs,
                 verbose=1,
                 validation_data=(x_test, y_test))

score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

print(
    "====================================================================================================="
)

n = 0
plt.imshow(x_test[n].reshape(28, 28), cmap='Greys', interpolation='nearest')
plt.show()

print('The Answer is ', model.predict_classes(x_test[n].reshape(
    (1, 28, 28, 1))))
コード例 #6
0
epochs = 5
batch_size = 128
history = autoencoder.fit(x_train, x_train, batch_size=batch_size, epochs=epochs, verbose=1,validation_data=(x_test, x_test))

#----------------------------------------------------模型可视化---------------------------------
SVG(model_to_dot(autoencoder).create(prog='dot',format='svg'))

#----------------------------------------------------查看自编码器的压缩效果------------------------
#只取编码器做模型(取输入层x和隐藏层h,作为网络结构)
encoded_imgs = model.predict(x_test)
#打印10张测试集手写体的压缩效果
n = 10
plt.figure(figsize=(20, 8))
for i in range(n):
    ax = plt.subplot(1, n, i+1)
    plt.imshow(encoded_imgs[i].reshape(4, 16).T) #将8*8的特征转化为4*16的图像
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()

#----------------------------------------------------查看自编码器的解码效果-------------------------
decoded_imgs = autoencoder.predict(x_test)    #decoded_imgs为输出层的效果
n = 10
plt.figure(figsize=(20, 6))
for i in range(n):
    #打印原图
    ax = plt.subplot(3, n, i+1)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
コード例 #7
0
rc('font', family='NanumBarunGothic')

wordcloud = WordCloud(
    font_path='/content/CookieRun Regular.ttf',  # 맥에선 한글폰트 설정 잘해야함.
    background_color='white',  # 배경 색깔 정하기
    colormap='Accent_r',  # 폰트 색깔 정하기
    width=800,
    height=800)

wordcloud_words = wordcloud.generate_from_frequencies(words)
array = wordcloud.to_array()
print(type(array))  # numpy.ndarray
print(array.shape)  # (800, 800, 3)

fig = plt.figure(figsize=(10, 10))
plt.imshow(array, interpolation="bilinear")
plt.axis('off')
plt.show()
fig.savefig('business_anlytics_worldcloud.png')
for sentence in lists:
    morphs.append(twitter.pos(sentence))
print(morphs)

noun_adj_adv_list = []
for sentence in morphs:
    for word, tag in sentence:
        if tag in ['Noun'] and ("것" not in word) and ("내" not in word) and (
                "나" not in word) and ("수" not in word) and (
                    "게" not in word) and ("말" not in word):
            noun_adj_adv_list.append(word)
コード例 #8
0
history = autoencoder.fit(x_train, x_train,
                          batch_size=batch_size,
                          epochs=epochs,
                          verbose=1,
                          validation_data=(x_test, x_test))

#------------------------------------------查看自编码器的压缩效果-----------------------------
#为隐藏层的结果(encoder的最后一层),只取编码器做模型
conv_encoder = Model(x, h)
encoded_imgs = autoencoder.predict(x_test)
#打印10张测试集手写体的压缩效果
n = 10
plt.figure(figsize=(20, 8))
for i in range(n):
    ax = plt.subplot(3, n, i+n+1)
    plt.imshow(encoded_imgs[i].reshape(4, 16).T)
    plt.gray()
    ax.get_xaxis().set_visible(False)
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()

#------------------------------------------查看自编码器的解码效果------------------------------
decoded_imgs = autoencoder.predict(x_test)
n = 10
plt.figure(figsize=(20,6))
for i in range(n):
    #原图
    ax = plt.subplot(3, n, i+1)
    plt.imshow(x_test[i].reshape(28, 28))