-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_military_resnet.py
102 lines (88 loc) · 3.52 KB
/
is_military_resnet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
created by: Donghyeon Won
"""
from __future__ import print_function
import os
import argparse
import numpy as np
import pandas as pd
import time
import shutil
from PIL import Image
from tqdm import tqdm
import torch
from torch.utils.data import Dataset, DataLoader
from torch.autograd import Variable
import torchvision.models as models
import torchvision.transforms as transforms
from util import pil_loader, modified_resnet50, ProtestDatasetEval, ProtestDatasetEvalSingle
#python3 pred.py --img_path /Users/Manav/Downloads/CalHacks6/protest-detection-violence-estimation/imgs/police-200.jpg --output_csvpath result.csv --model model_best.pth.tar
def eval_one_img(img, model):
"""
return model output of all the images in a directory
"""
model.eval()
num_workers = 4
batch_size = 16
dataset = ProtestDatasetEvalSingle(img = img)
data_loader = DataLoader(dataset,
num_workers = num_workers,
batch_size = batch_size)
outputs = []
for i, sample in enumerate(data_loader):
imgpath, input = sample['imgpath'], sample['image']
input_var = Variable(input)
output = model(input_var)
outputs.append(output.cpu().data.numpy())
df = pd.DataFrame(np.zeros((1, 12)))
df.columns = ["protest", "violence", "sign", "photo",
"fire", "police", "children", "group_20", "group_100",
"flag", "night", "shouting"]
df.iloc[:,:] = np.concatenate(outputs)
return df
def main():
# load trained model
print("*** loading model from {model}".format(model = args.model))
model = modified_resnet50()
if args.cuda:
model = model.cuda()
model.load_state_dict(torch.load(args.model, map_location='cpu')['state_dict'])
print("*** calculating the model output of the images in {img_dir}"
.format(img_dir = args.img_path))
#df = eval_one_dir('/Users/Manav/Downloads/CalHacks6/protest-detection-violence-estimation/new_imgs', model)
img = pil_loader(args.img_path)
df = eval_one_img(img, model)
# write csv file
#df.to_csv(args.output_csvpath, index = False)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--img_path",
type=str,
required = True,
help = "image path to calculate output of" )
parser.add_argument("--output_csvpath",
type=str,
default = "result.csv",
help = "path to output csv file"
)
parser.add_argument("--model",
type=str,
required = True,
help = "model path"
)
parser.add_argument("--cuda",
action = "store_true",
help = "use cuda?",
)
parser.add_argument("--workers",
type = int,
default = 4,
help = "number of workers",
)
parser.add_argument("--batch_size",
type = int,
default = 16,
help = "batch size",
)
args = parser.parse_args()
main()