Skip to content

Commit

Permalink
all files
Browse files Browse the repository at this point in the history
  • Loading branch information
fandulu committed Feb 14, 2020
1 parent 74de20b commit d96ff36
Show file tree
Hide file tree
Showing 23 changed files with 2,988 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.pyc
.ipynb_checkpoints
__pycache__
229 changes: 229 additions & 0 deletions 1.pano_detector.ipynb

Large diffs are not rendered by default.

407 changes: 407 additions & 0 deletions 2.tracking.ipynb

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions 3.generate_video.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"import glob"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"img_array = []\n",
"file_list = glob.glob('outputs/*.png')\n",
"file_list = np.sort(glob.glob('outputs/*.png'))\n",
"for filename in file_list:\n",
" img = cv2.imread(filename)\n",
" height, width, layers = img.shape\n",
" size = (width,height)\n",
" img_array.append(img)\n",
"\n",
"out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'), 24, size)\n",
" \n",
"for i in range(len(img_array)):\n",
" out.write(img_array[i])\n",
"out.release()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
148 changes: 148 additions & 0 deletions 4.smooth_tracking.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import argparse\n",
"import os\n",
"import sys\n",
"import cv2\n",
"import numpy as np\n",
"import glob\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib\n",
"from PIL import Image, ImageOps\n",
"import pandas as pd\n",
"from tqdm import tqdm\n",
"import json\n",
"import copy\n",
"from utils import *"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"with open('./data/pano_tracks_2.json', 'r') as f:\n",
" pano_tracks = json.load(f)\n",
" \n",
"with open('./data/pano_bridges_2.json', 'r') as f:\n",
" pano_bridges = json.load(f) "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"n_frames = len(pano_tracks)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"tracking_raw = {}\n",
"for frame in pano_tracks:\n",
" for instance in pano_tracks[frame]:\n",
" location = instance[:2]\n",
" track_id = instance[2]\n",
" if track_id not in list(tracking_raw.keys()):\n",
" tracking_raw[track_id] = []\n",
" tracking_raw[track_id].append([float(location[0]),float(location[1]),int(frame)])\n",
" else:\n",
" tracking_raw[track_id].append([float(location[0]),float(location[1]),int(frame)])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from scipy.ndimage.filters import gaussian_filter1d\n",
"\n",
"tracking_sm = {}\n",
"for k in tracking_raw:\n",
" location = tracking_raw[k]\n",
" if len(location)>5:\n",
" location = np.stack(location)\n",
" location[:,0] = gaussian_filter1d(location[:,0],0.5)\n",
" location[:,1] = gaussian_filter1d(location[:,1],0.5)\n",
" tracking_sm[k] = location\n",
" else:\n",
" tracking_sm[k] = location"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"tracking = {k:[] for k in range(2,n_frames+2)}"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"for k in tracking_sm:\n",
" track_id = k\n",
" for t in tracking_sm[k]:\n",
" frame = int(t[2])\n",
" tracking[frame].append([t[0],t[1],track_id])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"with open('./data/video_1_pano_smooth_2.json', 'w') as fp:\n",
" json.dump(tracking, fp)\n",
" \n",
"with open('./data/video_1_single_smooth_2.json', 'w') as fp:\n",
" json.dump(pano_bridges, fp)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
File renamed without changes
File renamed without changes
1 change: 1 addition & 0 deletions reid/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pth
5 changes: 5 additions & 0 deletions reid/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .baseline import Baseline

def build_encoder():
model = Baseline()
return model
6 changes: 6 additions & 0 deletions reid/backbones/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# encoding: utf-8
"""
@author: liaoxingyu
@contact: sherlockliao01@gmail.com
"""

150 changes: 150 additions & 0 deletions reid/backbones/resnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# encoding: utf-8
"""
@author: liaoxingyu
@contact: sherlockliao01@gmail.com
"""

import math

import torch
from torch import nn


def conv3x3(in_planes, out_planes, stride=1):
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=False)


class BasicBlock(nn.Module):
expansion = 1

def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride

def forward(self, x):
residual = x

out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)

out = self.conv2(out)
out = self.bn2(out)

if self.downsample is not None:
residual = self.downsample(x)

out += residual
out = self.relu(out)

return out


class Bottleneck(nn.Module):
expansion = 4

def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * 4)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride

def forward(self, x):
residual = x

out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)

out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)

out = self.conv3(out)
out = self.bn3(out)

if self.downsample is not None:
residual = self.downsample(x)

out += residual
out = self.relu(out)

return out


class ResNet(nn.Module):
def __init__(self, last_stride=2, block=Bottleneck, layers=[3, 4, 6, 3]):
self.inplanes = 64
super().__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
# self.relu = nn.ReLU(inplace=True) # add missed relu
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(
block, 512, layers[3], stride=last_stride)

def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)

layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))

return nn.Sequential(*layers)

def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
# x = self.relu(x) # add missed relu
x = self.maxpool(x)

x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)

return x

def load_param(self, model_path):
param_dict = torch.load(model_path)
for i in param_dict:
if 'fc' in i:
continue
self.state_dict()[i].copy_(param_dict[i])

def random_init(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()

Loading

0 comments on commit d96ff36

Please sign in to comment.