-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
20,465 additions
and
57 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import torch | ||
from albumentations.pytorch import ToTensorV2 | ||
import model_finetune | ||
import cv2 | ||
import albumentations as alb | ||
|
||
# 模型加载样例 | ||
weights = torch.load(r"output/resnet34-epoch=9-val_acc=0.966.ckpt")["state_dict"] # 模型权重 | ||
for k in list(weights.keys()): | ||
weights[str(k)[4:]]=weights.pop(k) | ||
|
||
net = model_finetune.ResNet("resnet34",30) | ||
net.load_state_dict(weights) # 加载权重字典 | ||
print(net) | ||
|
||
# 测试一张图片 | ||
image = cv2.imread(r"D:/Game_lsh/Gloabel_data/AID/Port\port_120.jpg", cv2.IMREAD_COLOR) | ||
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | ||
transforms_train = alb.Compose([ | ||
alb.Resize(height=224, width=224, p=1), | ||
alb.Normalize(p=1.0), | ||
ToTensorV2(p=1.0), | ||
]) | ||
image = transforms_train(image=image)['image'] | ||
image = torch.unsqueeze(image,dim=0) | ||
|
||
output = net(image) | ||
output = torch.softmax(output,dim=1) | ||
index =torch.argmax(output[0]).item() | ||
labels_dict = ['Airport', 'BareLand', 'BaseballField', 'Beach', 'Bridge', 'Center', 'Church', 'Commercial', 'DenseResidential', | ||
'Desert', 'Farmland', 'Forest', 'Industrial', 'Meadow', 'MediumResidential', 'Mountain', 'Park', 'Parking', | ||
'Playground', 'Pond', 'Port', 'RailwayStation', 'Resort', 'River', 'School', 'SparseResidential', 'Square', | ||
'Stadium', 'StorageTanks', 'Viaduct'] | ||
print(output) | ||
print(output[0,index].item(),labels_dict[index]) |