-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx3d-new-load.py
98 lines (64 loc) · 2.15 KB
/
x3d-new-load.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
#%%
import torch
from torch.fx import symbolic_trace
from torch import nn
trained_backbone = torch.load("output/x3d-moco-bdd/baseline/checkpoints/trained-backbone-ep200.pt",map_location=torch.device('cpu'))
trained_backbone = trained_backbone.eval()
trained_backbone
# %%
input_tensor = torch.zeros(3, 4, 182, 182)
preds = trained_backbone(input_tensor[None, None, ...])
preds
# %%
trained_backbone.head.projection = nn.Identity()
trained_backbone.head.act = nn.Identity()
trained_backbone.head
# %% Add new MLP head
# Layers
lin1 = nn.Linear(in_features=2048, out_features=2048, bias=True)
lin1_relu = nn.ReLU(inplace=True)
lin2 = nn.Linear(in_features=2048, out_features=2048, bias=True)
lin2_relu = nn.ReLU(inplace=True)
lin3 = nn.Linear(in_features=2048, out_features=128, bias=True)
lin3_relu = nn.ReLU(inplace=True)
# Forward propagation
x = trained_backbone(input_tensor[None, None, ...])
x = lin1(x)
x = lin1_relu(x)
x = lin2(x)
x = lin2_relu(x)
x = lin3(x)
x = lin3_relu(x)
x
#%% PPO
num_actions = 1
linear3_mean = nn.Linear(in_features=128, out_features=num_actions, bias=True)
linear3_var = nn.Linear(in_features=128, out_features=num_actions, bias=True)
tanh = nn.Tanh()
softplus = nn.Softplus()
mean = linear3_mean(x)
mean = tanh(mean)
print('shape of mean: ', mean)
var = linear3_var(x)
var = softplus(var)
print('shape of var: ', var)
x = torch.cat((mean, var), 1)
#x = torch.cat((mean, var))
# x = x.view(-1,1)
print("Shape of output in R50 actor:", x.shape)
# %%
# %%
from torch import nn
from collections import OrderedDict
x3d_head = nn.Sequential(OrderedDict([
('conv_5', nn.Conv3d(192, 432, kernel_size=(1, 1, 1), stride=(1, 1, 1), bias=False)),
('conv_5_bn', nn.BatchNorm3d(432, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)),
('conv_5_relu', nn.ReLU(inplace=True)),
('avg_pool', nn.AvgPool3d(kernel_size=[4, 5, 5], stride=1, padding=0)),
('lin_5', nn.Conv3d(432, 2048, kernel_size=(1, 1, 1), stride=(1, 1, 1), bias=False)),
('lin_5_relu', nn.ReLU(inplace=True)),
#('projection', nn.Linear(in_features=2048, out_features=128, bias=True)),
#('act', nn.Softmax(dim=4))
]))
x3d_head
# %%