def ResSENet101(num_classes = 10): ''' First conv layer: 1 4 residual blocks with [3, 4, 23, 3] sets of three convolutions each: 3*3 + 4*3 + 23*3 + 3*3 = 99 last FC layer: 1 Total layers: 1+99+1 = 101 ''' return ResNet(ResidualSEBasicBlock, [3, 4, 23, 3], num_classes)
def ResSENet50(num_classes = 10): ''' First conv layer: 1 4 residual blocks with [3, 4, 6, 3] sets of three convolutions each: 3*3 + 4*3 + 6*3 + 3*3 = 48 last FC layer: 1 Total layers: 1+48+1 = 50 ''' return ResNet(ResidualSEBasicBlock, [3, 4, 6, 3], num_classes)
def ResSENet152(num_classes = 10): ''' First conv layer: 1 4 residual blocks with [3, 8, 36, 3] sets of three convolutions each: 3*3 + 8*3 + 36*3 + 3*3 = 150 last FC layer: 1 Total layers: 1+150+1 = 152 ''' return ResNet(ResidualSEBasicBlock, [3, 8, 36, 3], num_classes)
def ResNet18(num_classes=10): ''' First conv layer: 1 4 residual blocks with two sets of two convolutions each: 2*2 + 2*2 + 2*2 + 2*2 = 16 conv layers last FC layer: 1 Total layers: 1+16+1 = 18 ''' return ResNet(BasicBlock, [2, 2, 2, 2], num_classes)
def ResNet34(num_classes): ''' First conv layer: 1 4 residual blocks with [3, 4, 6, 3] sets of two convolutions each: 3*2 + 4*2 + 6*2 + 3*2 = 32 last FC layer: 1 Total layers: 1+32+1 = 34 ''' return ResNet(BasicBlock, [3, 4, 6, 3], num_classes)
with torch.no_grad(): for i, j in loader: inputs, labels = i.to(device), j.to(device) outputs = model(inputs) correct = correct + accuracy(outputs, labels) print("[%d/%d] Test Accuracy : %f" % (epochs, total_epochs, (correct / len(loader.dataset)) * 100)) print( '---------------------------------------------------------------------' ) return (correct / len(loader.dataset)) * 100 dtype = torch.cuda.FloatTensor torch.manual_seed(52) net = ResNet(BasicBlock, [2, 2, 2, 2]).to(device) named_layers = [] for i in net.named_parameters(): named_layers.append(i[0]) opt = SwatsLocal(net.parameters(), named_layers, lr=0.001) loss = nn.CrossEntropyLoss().type(dtype) def adjust_lr(opt, epochs): base_lr = 0.001 if epochs >= 75: for ui in opt.param_groups: ui['div_lr_decay'] = 10 if epochs >= 150: for ui in opt.param_groups: ui['div_lr_decay'] = 100
def ResSENet18(num_classes = 10): return ResNet(ResidualSEBasicBlock, [2, 2, 2, 2], num_classes)
def Resnet101(num_classes=1): model = ResNet(Bottleneck, [3, 4, 23, 3], num_classes=num_classes) return model
def CBAMResnet101(num_classes=1): model = ResNet(CBAMBottleneck, [3, 4, 23, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) return model
def SEResnet50(num_classes=1): model = ResNet(SEBottleneck, [3, 4, 6, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) return model
from Resnet import ResNet # Constants # Setting SEED = 222 BATCH_SIZE = 64 HEIGHT = 137 WIDTH = 236 device = "cuda" if torch.cuda.is_available() else "cpu" # Call seed to setup for repeatable results seed_everything(SEED) # Create Model model = ResNet().to(device) # Setup data files to load data_dir = '../data' files_train = [f'train_image_data_{fid}.parquet' for fid in range(4)] # Predict Test values model.eval() row_id = [] target = [] dataset = BenDataset(HEIGHT, WIDTH) for i in range(4): # Do training here parquet_df = pd.read_parquet(F'../data/train_image_data_{i}.parquet', engine='pyarrow')
def __init__(self): self.resnet_head = ResNet() self.input = tf.placeholder(tf.float32, shape=(None, 400, 400, 3)) self.labels = tf.placeholder(tf.float32, shape=(None, )) self.output = self.forward(self.input) self.lr = 0.0001