from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import argparse
import torch.utils.data as data
from torch.utils.data import DataLoader, Dataset
import numpy as np
import cv2
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '6, 7'
class xu_net(nn.Module):
def __init__(self):
super(xu_net, self).__init__()
self.conv1 = nn.Conv2d(1, 8, 5)
self.conv2 = nn.Conv2d(8, 16, 5)
self.conv3 = nn.Conv2d(16, 32, 1)
self.conv4 = nn.Conv2d(32, 64, 1)
self.conv5 = nn.Conv2d(64, 128, 1)
self.fc = nn.Linear(128, 2)
self.bn1 = nn.BatchNorm2d(8)
self.bn2 = nn.BatchNorm2d(16)
self.bn3 = nn.BatchNorm2d(32)
self.bn4 = nn.BatchNorm2d(64)
self.bn5 = nn.BatchNorm2d(128)
self.avg_pool1_4 = nn.AvgPool2d(5, 2)
def forward(self, x):
F_0 = np.array([[[[-1 / 12.]], [[2 / 12.]], [[-2 / 12.]], [[2 / 12.]], [[-1 / 12.]]],
[[[2 / 12.]], [[-6 / 12.]], [[8 / 12.]], [[-6 / 12.]], [[2 / 12.]]],
[[[-2 / 12.]], [[8 / 12.]], [[-12 / 12.]], [[8 / 12.]], [[-2 / 12.]]],
[[[2 / 12.]], [[-6 / 12.]], [[8 / 12.]], [[-6 / 12.]], [[2 / 12.]]],
[[[-1 / 12.]], [[2 / 12.]], [[-2 / 12.]], [[2 / 12.]], [[-1 / 12.]]]], dtype=np.float)
F_0 = F_0.reshape((1, 1, 5, 5))
F_0 = torch.from_numpy(F_0).to(torch.device('cuda'), dtype=torch.float)
x = x.reshape((-1, 1, 256, 256)) # x shape, batch, channel, height, width
# group 1
x = F.conv2d(x, F_0, padding=2)
x = self.conv1(x) ###
x = torch.abs(x)
x = self.bn1(x)
pytorch实现CNN模型(spatial XuNet)训练模型
最新推荐文章于 2024-04-04 00:46:43 发布