Pytorch預(yù)訓(xùn)練模型浓冒、內(nèi)置模型實(shí)現(xiàn)圖像分類栽渴、檢測(cè)和分割

原創(chuàng):余曉龍

Pytorch中提供了很多已經(jīng)在ImageNet數(shù)據(jù)集上訓(xùn)練好的模型了,可以直接被加載到模型中進(jìn)行預(yù)測(cè)任務(wù)稳懒。預(yù)訓(xùn)練模型存放在Pytorch的torchvision中庫(kù)闲擦,在torchvision庫(kù)的models模塊下可以查看內(nèi)置的模型,models模塊中的模型包含四大類场梆,如圖所示:

一墅冷、圖像分類代碼實(shí)現(xiàn)

# coding: utf-8

from PIL import Image
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 步驟一(替換sans-serif字體)
plt.rcParams['axes.unicode_minus'] = False   # 步驟二(解決坐標(biāo)軸負(fù)數(shù)的負(fù)號(hào)顯示問(wèn)題)
import json
import numpy as np

import torch
import torch.nn.functional as F
from torchvision import models, transforms

# 1.下載并加載預(yù)訓(xùn)練模型
model = models.resnet18(pretrained=True)
model = model.eval()

# 2.加載標(biāo)簽并對(duì)輸入數(shù)據(jù)進(jìn)行處理
labels_path = './imagenet_class_index.json'
with open(labels_path) as json_data:
    idx2labels = json.load(json_data)
# print(idx2labels)


def getone(onestr):
    return onestr.replace(',', ' ')


# 加載中文標(biāo)簽
with open('./zh_label.csv', 'r+', encoding='gbk') as f:
    # print(f)
    # print(map(getone, list(f)))
    zh_labels = list(map(getone, list(f)))

    print(len(zh_labels), type(zh_labels), zh_labels[:5])


transform = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]
    )
])

# 3.使用模型進(jìn)行預(yù)測(cè)
def preimg(img):
    if img.mode == 'RGBA':
        ch = 4
        a = np.asarray(img)[:, :, :3]
        img = Image.fromarray(a)
    return img


im = preimg(Image.open('panda.jpg'))
transformed_img = transform(im)

inputimg = transformed_img.unsqueeze(0)

output = model(inputimg)
output = F.softmax(output, dim=1)

prediction_score, pred_label_idx = torch.topk(output, 3)
prediction_score = prediction_score.detach().numpy()[0]
print(prediction_score[0])

pred_label_idx = pred_label_idx.detach().numpy()[0]
print(pred_label_idx)

predicted_label = idx2labels[str(pred_label_idx[0])][1]
print(predicted_label)

predicted_label_zh = zh_labels[pred_label_idx[0] + 1]
print(predicted_label_zh)

# 4.預(yù)測(cè)結(jié)果可視化
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 8))
fig.sca(ax1)
ax1.imshow(im)
plt.xticks([])
plt.yticks([])

barlist = ax2.bar(range(3), [i for i in prediction_score])
barlist[0].set_color('g')

plt.sca(ax2)
plt.ylim([0, 1.1])

plt.xticks(range(3),
           # [idx2labels[str(i)][1] for i in pred_label_idx],
           [zh_labels[pred_label_idx[i] + 1] for i in range(3)],
           rotation='45')
fig.subplots_adjust(bottom=0.2)
plt.show()
圖像分類結(jié)果圖

輸入一張熊貓圖片,右邊輸出模型的預(yù)測(cè)結(jié)果或油,如上圖所示寞忿。

二、目標(biāo)檢測(cè)代碼實(shí)現(xiàn)

from PIL import Image
import matplotlib.pyplot as plt
import torch
import torchvision.transforms as T
import torchvision
import torch
import numpy as np
import cv2
import random

# 加載maskrcnn模型進(jìn)行目標(biāo)檢測(cè)
model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)
model.eval()
COCO_INSTANCE_CATEGORY_NAMES = [
    '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
    'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign',
    'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
    'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A', 'N/A',
    'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
    'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
    'bottle', 'N/A', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
    'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
    'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table',
    'N/A', 'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
    'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book',
    'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
]


def get_prediction(img_path, threshold):
    img = Image.open(img_path)
    transform = T.Compose([T.ToTensor()])
    img = transform(img)
    pred = model([img])
    print('pred')
    print(pred)
    pred_score = list(pred[0]['scores'].detach().numpy())
    pred_t = [pred_score.index(x) for x in pred_score if x > threshold][-1]
    print("masks>0.5")
    print(pred[0]['masks'] > 0.5)
    masks = (pred[0]['masks'] > 0.5).squeeze().detach().cpu().numpy()
    print("this is masks")
    print(masks)
    pred_class = [COCO_INSTANCE_CATEGORY_NAMES[i] for i in list(pred[0]['labels'].numpy())]
    pred_boxes = [[(i[0], i[1]), (i[2], i[3])] for i in list(pred[0]['boxes'].detach().numpy())]
    masks = masks[:pred_t + 1]
    pred_boxes = pred_boxes[:pred_t + 1]
    pred_class = pred_class[:pred_t + 1]
    return masks, pred_boxes, pred_class


def random_colour_masks(image):
    colours = [[0, 255, 0], [0, 0, 255], [255, 0, 0], [0, 255, 255], [255, 255, 0], [255, 0, 255], [80, 70, 180],
               [250, 80, 190], [245, 145, 50], [70, 150, 250], [50, 190, 190]]
    r = np.zeros_like(image).astype(np.uint8)
    g = np.zeros_like(image).astype(np.uint8)
    b = np.zeros_like(image).astype(np.uint8)
    r[image == 1], g[image == 1], b[image == 1] = colours[random.randrange(0, 10)]
    coloured_mask = np.stack([r, g, b], axis=2)
    return coloured_mask


def instance_segmentation_api(img_path, threshold=0.5, rect_th=3, text_size=10, text_th=3):
    masks, boxes, pred_cls = get_prediction(img_path, threshold)
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    for i in range(len(masks)):
        rgb_mask, randcol = random_colour_masks(masks[i]), (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        img = cv2.addWeighted(img, 1, rgb_mask, 0.5, 0)
        cv2.rectangle(img, boxes[i][0], boxes[i][1], color=randcol, thickness=rect_th)
        cv2.putText(img, pred_cls[i], boxes[i][0], cv2.FONT_HERSHEY_SIMPLEX, text_size, randcol, thickness=text_th)
    plt.figure(figsize=(20, 30))
    plt.imshow(img)
    plt.xticks([])
    plt.yticks([])
    plt.show()


instance_segmentation_api('./horse.jpg')
原始圖像
maskrcnn模型預(yù)測(cè)圖片

三顶岸、語(yǔ)義分割代碼實(shí)現(xiàn)

import torch
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
from torchvision import models
from torchvision import transforms

# 加載deeplabv3模型進(jìn)行語(yǔ)義分割
model = models.segmentation.deeplabv3_resnet101(pretrained=True)
model = model.eval()

transform = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]
    )
])

def preimg(img):
    if img.mode == 'RGBA':
        ch = 4
        a = np.asarray(img)[:, :, :3]
        img = Image.fromarray(a)
    return img

img = Image.open('./horse.jpg')
plt.imshow(img)
plt.axis('off')
plt.show()

im = preimg(img)

inputimg = transform(im).unsqueeze(0)

tt = np.transpose(inputimg.detach().numpy()[0], (1, 2, 0))
plt.imshow(tt)
plt.axis('off')
plt.show()


output = model(inputimg)
print(output['out'].shape)


output = torch.argmax(output['out'].squeeze(),
                      dim=0).detach().cpu().numpy()

resultclass = set(list(output.flat))
print(resultclass)


def decode_segmap(image, nc=21):
    label_colors = np.array(
        [(0, 0, 0), (128, 0, 0), (0, 128, 0), (128, 128, 0),
         (0, 0, 128), (128, 0, 128), (0, 128, 128), (128, 128, 128),
         (64, 0, 0), (192, 0, 0), (64, 128, 0), (192, 128, 0),
         (64, 0, 128), (192, 0, 128), (64, 128, 128), (192, 128, 128),
         (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128)]

    )
    r = np.zeros_like(image).astype(np.uint8)
    g = np.zeros_like(image).astype(np.uint8)
    b = np.zeros_like(image).astype(np.uint8)

    for l in range(0, nc):
        idx = image == l
        r[idx] = label_colors[l, 0]
        g[idx] = label_colors[l, 1]
        b[idx] = label_colors[l, 2]

    return np.stack([r, g, b], axis=2)


rgb = decode_segmap(output)
print(rgb)

img = Image.fromarray(rgb)
plt.axis('off')
plt.imshow(img)
plt.show()
預(yù)處理后的圖片

!
語(yǔ)義分割后的圖片

模型從圖中識(shí)別出了兩個(gè)類別的內(nèi)容腔彰,索引值分別為13、15辖佣,所對(duì)應(yīng)的分類名稱是馬和人萍桌。調(diào)用函數(shù),通過(guò)對(duì)預(yù)測(cè)結(jié)果進(jìn)行染色凌简,得到的預(yù)測(cè)結(jié)果如上圖所示上炎。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子藕施,更是在濱河造成了極大的恐慌寇损,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件裳食,死亡現(xiàn)場(chǎng)離奇詭異矛市,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)诲祸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門浊吏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人救氯,你說(shuō)我怎么就攤上這事找田。” “怎么了着憨?”我有些...
    開(kāi)封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵墩衙,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我甲抖,道長(zhǎng)漆改,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任准谚,我火速辦了婚禮挫剑,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘柱衔。我一直安慰自己樊破,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布秀存。 她就那樣靜靜地躺著,像睡著了一般羽氮。 火紅的嫁衣襯著肌膚如雪或链。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天档押,我揣著相機(jī)與錄音澳盐,去河邊找鬼。 笑死令宿,一個(gè)胖子當(dāng)著我的面吹牛叼耙,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播粒没,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼筛婉,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起爽撒,我...
    開(kāi)封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤入蛆,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后硕勿,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體哨毁,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年源武,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了扼褪。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡粱栖,死狀恐怖话浇,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情查排,我是刑警寧澤凳枝,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站跋核,受9級(jí)特大地震影響岖瑰,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜砂代,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一蹋订、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧刻伊,春花似錦露戒、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至丁屎,卻和暖如春荠锭,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背晨川。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工证九, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人共虑。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓愧怜,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親妈拌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子拥坛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容