my_kaggle_solution's People
my_kaggle_solution's Issues
My model
from torchvision import models
from torch import nn
import torch
import pretrainedmodels
class AdaptiveConcatPool2d(nn.Module):
def init(self, sz=None):
super().init()
sz = sz or (1,1)
self.ap = nn.AdaptiveAvgPool2d(sz)
self.mp = nn.AdaptiveMaxPool2d(sz)
def forward(self, x):
return torch.cat([self.mp(x), self.ap(x)], 1)
class ConvBnRelu(nn.Module):
def init(self, in_channels, out_channels):
super().init()
self.conv = nn.Sequential(nn.Conv2d(in_channels, out_channels, 3, padding=1),
nn.BatchNorm2d(out_channels),
nn.LeakyReLU(inplace=True)
)
def forward(self, x):
return self.conv(x)
class myModel(nn.Module):
def init(self):
super(myModel, self).init()
# RestNet_model = pretrainedmodels.se_resnext50_32x4d(num_classes=1000, pretrained='imagenet')
RestNet_model = models.resnet34(pretrained=True)
# weigth_conv1 = RestNet_model.layer0.conv1.weight
weigth_conv1 = RestNet_model.conv1.weight
self.Conv1 = nn.Conv2d(4,64,kernel_size=(7,7),stride=(2,2),padding=(3, 3), bias=False)
self.Conv1.weight = torch.nn.Parameter(torch.cat((weigth_conv1,weigth_conv1[:,:1,:,:]),dim = 1))
self.bnconv = nn.BatchNorm2d(64)
self.reluconv = nn.ReLU(inplace=True)
self.poolconv = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.feature = nn.Sequential(*list(RestNet_model.children())[1:-2])
self.feature1 = nn.Sequential(RestNet_model.layer1,RestNet_model.layer2)
self.feature2 = nn.Sequential(RestNet_model.layer3,RestNet_model.layer4)
self.pool = AdaptiveConcatPool2d()
self.fc = nn.Sequential(
torch.nn.Linear(in_features=1024, out_features=512, bias=True),
torch.nn.Linear(in_features=512, out_features=28, bias=True),
nn.BatchNorm1d(1024, momentum=0.5),
nn.BatchNorm1d(512, momentum=0.5),
nn.Dropout(p=0.5)
)
self.pool = AdaptiveConcatPool2d()
self.bn1 = nn.BatchNorm1d(1024, momentum=0.5)
self.dropout1 = nn.Dropout(p=0.5)
self.Linear1 = torch.nn.Linear(in_features=1024, out_features=512, bias=True)
self.relu = nn.ReLU()
self.Linear2 = torch.nn.Linear(in_features=512, out_features=28, bias=True)
self.bn2 = nn.BatchNorm1d(512, momentum=0.5)
self.dropout2 = nn.Dropout(p=0.5)
def forward(self, x):
print("f.szie", x.size())
x = self.Conv1(x)
gap1 = x
print("Cnov1 ", x.size())
x = self.bnconv(x)
x = self.reluconv(x)
x = self.poolconv(x)
print("feature1 ", x.size())
x = self.feature1(x)
print("feature2 ", x.size())
x = self.feature2(x)
gapin1 = self.pool(gap1)
print("feature ", gapin1.size())
x = self.pool(x)
print("pool ", x.size())
x = torch.cat([gapin1,x],1)
print("pool2 ", x.size())
x = x.view(-1, 1024)
print("view ", x.size())
#x = self.fc(x)
x = self.bn1(x)
x = self.dropout1(x)
x = self.Linear1(x)
x = self.relu(x)
x = self.bn2(x)
x = self.dropout2(x)
x = self.Linear2(x)
print("Linear ", x.size())
return x
#print(pretrainedmodels.model_names)
#RestNet_model = pretrainedmodels.se_resnet50(num_classes=1000, pretrained='imagenet')
#print(nn.Sequential(*list(RestNet_model.children())[1:-1]))
#net = myModel()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = myModel().to(device)
input = torch.FloatTensor(32, 4, 512, 512).to(device)
output = model(input)
#RestNet_model = models.resnet34(pretrained=True)
#weigth_conv1 = RestNet_model.conv1.weight
#weigth_conv2 = torch.nn.Parameter(torch.cat((weigth_conv1, weigth_conv1[:, :1, :, :]), dim=1))
#print("layer1")
#print(RestNet_model.layer1)
#print("layer2")
#print(RestNet_model.layer2)
#print("layer3")
#print(RestNet_model.layer3)
#print("layer4")
#print(RestNet_model.layer4)
#print(nn.Sequential(*list(model.children())[3:]))
#print('Conv2x 3x')
#print(nn.Sequential(*list(model.feature.children())[3:5]))
#print('Conv3x 4x')
#print(nn.Sequential(*list(model.feature.children())[5:7]))
#print(nn.Sequential(*list(model.children())[3:]))
#print(weigth_conv2.size())
#print(nn.Sequential(*list(RestNet_model.children())[1:]))
class weights
Learning rate
model = nn.Linear(10, 2)
optimizer = optim.SGD(model.parameters(), lr=1.)
steps = 10
scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, steps)
for epoch in range(5):
for idx in range(steps):
scheduler.step()
print(scheduler.get_lr())
print('Reset scheduler')
scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, steps)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.