File args_util.py added (mode: 100644) (index 0000000..8d7d60a) |
|
1 |
|
""" |
|
2 |
|
contain dummy args with config |
|
3 |
|
helpfull for copy paste Kaggle |
|
4 |
|
""" |
|
5 |
|
import argparse |
|
6 |
|
|
|
7 |
|
|
|
8 |
|
def make_args(gpu="0", task="task_one_"): |
|
9 |
|
""" |
|
10 |
|
these arg does not have any required commandline arg (all with default value) |
|
11 |
|
:param train_json: |
|
12 |
|
:param test_json: |
|
13 |
|
:param pre: |
|
14 |
|
:param gpu: |
|
15 |
|
:param task: |
|
16 |
|
:return: |
|
17 |
|
""" |
|
18 |
|
parser = argparse.ArgumentParser(description='PyTorch CSRNet') |
|
19 |
|
|
|
20 |
|
args = parser.parse_args() |
|
21 |
|
args.gpu = gpu |
|
22 |
|
args.task = task |
|
23 |
|
args.pre = None |
|
24 |
|
return args |
|
25 |
|
|
|
26 |
|
class Meow(): |
|
27 |
|
def __init__(self): |
|
28 |
|
pass |
|
29 |
|
|
|
30 |
|
|
|
31 |
|
def make_meow_args(gpu="0", task="task_one_"): |
|
32 |
|
args = Meow() |
|
33 |
|
args.gpu = gpu |
|
34 |
|
args.task = task |
|
35 |
|
args.pre = None |
|
36 |
|
return args |
|
37 |
|
|
|
38 |
|
|
|
39 |
|
def real_args_parse(): |
|
40 |
|
""" |
|
41 |
|
this is not dummy |
|
42 |
|
if you are going to make all-in-one notebook, ignore this |
|
43 |
|
:return: |
|
44 |
|
""" |
|
45 |
|
parser = argparse.ArgumentParser(description='CrowdCounting') |
|
46 |
|
parser.add_argument("--task_id", action="store", default="dev") |
|
47 |
|
parser.add_argument('-a', action="store_true", default=False) |
|
48 |
|
|
|
49 |
|
parser.add_argument('--input', action="store", type=str) |
|
50 |
|
parser.add_argument('--output', action="store", type=str) |
|
51 |
|
parser.add_argument('--model', action="store", default="csrnet") |
|
52 |
|
arg = parser.parse_args() |
|
53 |
|
return arg |
File data_flow.py added (mode: 100644) (index 0000000..9d167b4) |
|
1 |
|
import os |
|
2 |
|
import glob |
|
3 |
|
from sklearn.model_selection import train_test_split |
|
4 |
|
import json |
|
5 |
|
import random |
|
6 |
|
import os |
|
7 |
|
from PIL import Image, ImageFilter, ImageDraw |
|
8 |
|
import numpy as np |
|
9 |
|
import h5py |
|
10 |
|
from PIL import ImageStat |
|
11 |
|
import cv2 |
|
12 |
|
import os |
|
13 |
|
import random |
|
14 |
|
import torch |
|
15 |
|
import numpy as np |
|
16 |
|
from torch.utils.data import Dataset |
|
17 |
|
from PIL import Image |
|
18 |
|
import torchvision.transforms.functional as F |
|
19 |
|
|
|
20 |
|
|
|
21 |
|
""" |
|
22 |
|
create a list of file (full directory) |
|
23 |
|
""" |
|
24 |
|
|
|
25 |
|
def create_training_image_list(data_path): |
|
26 |
|
""" |
|
27 |
|
create a list of absolutely path of jpg file |
|
28 |
|
:param data_path: must contain subfolder "images" with *.jpg (example ShanghaiTech/part_A/train_data/) |
|
29 |
|
:return: |
|
30 |
|
""" |
|
31 |
|
DATA_PATH = data_path |
|
32 |
|
image_path_list = glob.glob(os.path.join(DATA_PATH, "images", "*.jpg")) |
|
33 |
|
return image_path_list |
|
34 |
|
|
|
35 |
|
|
|
36 |
|
def get_train_val_list(data_path): |
|
37 |
|
DATA_PATH = data_path |
|
38 |
|
image_path_list = glob.glob(os.path.join(DATA_PATH, "images", "*.jpg")) |
|
39 |
|
train, val = train_test_split(image_path_list, test_size=0.1) |
|
40 |
|
|
|
41 |
|
print("train size ", len(train)) |
|
42 |
|
print("val size ", len(val)) |
|
43 |
|
return train, val |
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
|
def load_data(img_path, train=True): |
|
49 |
|
gt_path = img_path.replace('.jpg', '.h5').replace('images', 'ground-truth-h5') |
|
50 |
|
img = Image.open(img_path).convert('RGB') |
|
51 |
|
gt_file = h5py.File(gt_path, 'r') |
|
52 |
|
target = np.asarray(gt_file['density']) |
|
53 |
|
|
|
54 |
|
target = cv2.resize(target, (int(target.shape[1] / 8), int(target.shape[0] / 8)), |
|
55 |
|
interpolation=cv2.INTER_CUBIC) * 64 |
|
56 |
|
|
|
57 |
|
return img, target |
|
58 |
|
|
|
59 |
|
class ListDataset(Dataset): |
|
60 |
|
def __init__(self, root, shape=None, shuffle=True, transform=None, train=False, seen=0, batch_size=1, |
|
61 |
|
num_workers=4): |
|
62 |
|
""" |
|
63 |
|
if you have different image size, then batch_size must be 1 |
|
64 |
|
:param root: |
|
65 |
|
:param shape: |
|
66 |
|
:param shuffle: |
|
67 |
|
:param transform: |
|
68 |
|
:param train: |
|
69 |
|
:param seen: |
|
70 |
|
:param batch_size: |
|
71 |
|
:param num_workers: |
|
72 |
|
""" |
|
73 |
|
if train: |
|
74 |
|
root = root * 4 |
|
75 |
|
if shuffle: |
|
76 |
|
random.shuffle(root) |
|
77 |
|
|
|
78 |
|
self.nSamples = len(root) |
|
79 |
|
self.lines = root |
|
80 |
|
self.transform = transform |
|
81 |
|
self.train = train |
|
82 |
|
self.shape = shape |
|
83 |
|
self.seen = seen |
|
84 |
|
self.batch_size = batch_size |
|
85 |
|
self.num_workers = num_workers |
|
86 |
|
|
|
87 |
|
def __len__(self): |
|
88 |
|
return self.nSamples |
|
89 |
|
|
|
90 |
|
def __getitem__(self, index): |
|
91 |
|
assert index <= len(self), 'index range error' |
|
92 |
|
img_path = self.lines[index] |
|
93 |
|
img, target = load_data(img_path, self.train) |
|
94 |
|
if self.transform is not None: |
|
95 |
|
img = self.transform(img) |
|
96 |
|
return img, target |
File model_util.py added (mode: 100644) (index 0000000..de7b192) |
|
1 |
|
import h5py |
|
2 |
|
import torch |
|
3 |
|
import shutil |
|
4 |
|
|
|
5 |
|
def save_net(fname, net): |
|
6 |
|
with h5py.File(fname, 'w') as h5f: |
|
7 |
|
for k, v in net.state_dict().items(): |
|
8 |
|
h5f.create_dataset(k, data=v.cpu().numpy()) |
|
9 |
|
|
|
10 |
|
|
|
11 |
|
def load_net(fname, net): |
|
12 |
|
with h5py.File(fname, 'r') as h5f: |
|
13 |
|
for k, v in net.state_dict().items(): |
|
14 |
|
param = torch.from_numpy(np.asarray(h5f[k])) |
|
15 |
|
v.copy_(param) |
|
16 |
|
|
|
17 |
|
|
|
18 |
|
def save_checkpoint(state, is_best, task_id, filename='checkpoint.pth.tar'): |
|
19 |
|
torch.save(state, task_id + filename) |
|
20 |
|
if is_best: |
|
21 |
|
shutil.copyfile(task_id + filename, task_id + 'model_best.pth.tar') |
File models/CSRNet.py added (mode: 100644) (index 0000000..fbcdcc4) |
|
1 |
|
import torch.nn as nn |
|
2 |
|
import torch |
|
3 |
|
from torchvision import models |
|
4 |
|
|
|
5 |
|
class CSRNet(nn.Module): |
|
6 |
|
def __init__(self, load_weights=False): |
|
7 |
|
super(CSRNet, self).__init__() |
|
8 |
|
self.seen = 0 |
|
9 |
|
self.frontend_feat = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512] |
|
10 |
|
self.backend_feat = [512, 512, 512, 256, 128, 64] |
|
11 |
|
self.frontend = make_layers(self.frontend_feat) |
|
12 |
|
self.backend = make_layers(self.backend_feat, in_channels=512, dilation=True) |
|
13 |
|
self.output_layer = nn.Conv2d(64, 1, kernel_size=1) |
|
14 |
|
if not load_weights: |
|
15 |
|
mod = models.vgg16(pretrained=True) |
|
16 |
|
self._initialize_weights() |
|
17 |
|
for i in range(len(list(self.frontend.state_dict().items()))): |
|
18 |
|
list(self.frontend.state_dict().items())[i][1].data[:] = list(mod.state_dict().items())[i][1].data[:] |
|
19 |
|
|
|
20 |
|
def forward(self, x): |
|
21 |
|
x = self.frontend(x) |
|
22 |
|
x = self.backend(x) |
|
23 |
|
x = self.output_layer(x) |
|
24 |
|
return x |
|
25 |
|
|
|
26 |
|
def _initialize_weights(self): |
|
27 |
|
for m in self.modules(): |
|
28 |
|
if isinstance(m, nn.Conv2d): |
|
29 |
|
nn.init.normal_(m.weight, std=0.01) |
|
30 |
|
if m.bias is not None: |
|
31 |
|
nn.init.constant_(m.bias, 0) |
|
32 |
|
elif isinstance(m, nn.BatchNorm2d): |
|
33 |
|
nn.init.constant_(m.weight, 1) |
|
34 |
|
nn.init.constant_(m.bias, 0) |
|
35 |
|
|
|
36 |
|
|
|
37 |
|
def make_layers(cfg, in_channels=3, batch_norm=False, dilation=False): |
|
38 |
|
if dilation: |
|
39 |
|
d_rate = 2 |
|
40 |
|
else: |
|
41 |
|
d_rate = 1 |
|
42 |
|
layers = [] |
|
43 |
|
for v in cfg: |
|
44 |
|
if v == 'M': |
|
45 |
|
layers += [nn.MaxPool2d(kernel_size=2, stride=2)] |
|
46 |
|
else: |
|
47 |
|
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=d_rate, dilation=d_rate) |
|
48 |
|
if batch_norm: |
|
49 |
|
layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)] |
|
50 |
|
else: |
|
51 |
|
layers += [conv2d, nn.ReLU(inplace=True)] |
|
52 |
|
in_channels = v |
|
53 |
|
return nn.Sequential(*layers) |