現在、自分のローカルPC内に保存している画像データを用いて、efficientnet実装試みています。
データセットを入力する段階で、うまく出力されなくなってしまいました。
ネット上で参考になる記事についても検索いたしましたが、どれもCIFAR10等を用いたものですのであまり参考になりませんでした。
初歩的なことなのでしょうが、ご教示願えますでしょうか。
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-14-80a768b5b891> in <module> 5 transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2470, 0.2435, 0.2616]) 6 ]) ----> 7 train_ds = '.ora/Desktop/dataset2/train'(train=True, transform=train_transform, download=True) 8 train_dl = torch.utils.data.DataLoader(train_ds, batch_size=128, num_workers=4, pin_memory=True) TypeError: 'str' object is not callable
該当のソースコード
python
1import torch 2import torch.nn as nn 3import torch.nn.functional as F 4import torchvision 5import torchvision.transforms as transforms 6import tqdm 7import matplotlib.pyplot as plt 8 9class Swish(nn.Module): 10 """x*sigmoid(x)""" 11 def __init__(self): 12 super().__init__() 13 def forward(self, x): 14 return x*torch.sigmoid(x) 15 16class DWBlock(nn.Module): 17 def __init__(self, in_c, k, s, p, bias=False): 18 if p != k//2: 19 print("output may not be the same spatial size as input") 20 super().__init__() 21 self.dw = nn.Conv2d(in_c, in_c, kernel_size=k, stride=s, padding=p, groups=in_c, bias=bias) 22 self.bn = nn.BatchNorm2d(in_c) 23 self.act = Swish() 24 25 def forward(self, x): 26 out = self.act(self.bn(self.dw(x))) 27 return out 28 29class PWBlock(nn.Module): 30 def __init__(self, in_c, out_c, bias=False, act="swish"): 31 super().__init__() 32 self.dw = nn.Conv2d(in_c, out_c, kernel_size=1, bias=bias) 33 self.bn = nn.BatchNorm2d(out_c) 34 if act=='swish': 35 self.act = Swish() 36 elif act is None: 37 self.act = nn.Identity() 38 39 def forward(self, x): 40 out = self.act(self.bn(self.dw(x))) 41 return out 42 43class SEBlock(nn.Module): 44 def __init__(self, in_c, h=8): 45 super().__init__() 46 # Squeeze 47 self.gap = nn.AdaptiveAvgPool2d(1) 48 # Excitation 49 self.fc1 = nn.Linear(in_c, in_c//h, bias=False) 50 self.act1 = Swish() 51 self.fc2 = nn.Linear(in_c//h, in_c, bias=False) 52 self.act2 = nn.Sigmoid() 53 54 def forward(self, x): 55 out = self.gap(x).squeeze(-1).squeeze(-1) 56 out = self.act1(self.fc1(out)) 57 out = self.act2(self.fc2(out)).unsqueeze(-1).unsqueeze(-1) 58 return out*x 59 60class MBConv(nn.Module): 61 def __init__(self, in_c, out_c, k=5, s=1, expansion=1): 62 super().__init__() 63 self.s = s 64 self.in_c = in_c 65 self.out_c = out_c 66 self.pw1 = PWBlock(in_c, in_c*expansion, bias=False) 67 self.dw = DWBlock(in_c*expansion, k=k, s=s, p=k//2, bias=False) 68 self.se = SEBlock(in_c*expansion) 69 self.pw2 = PWBlock(in_c*expansion, out_c, bias=False, act=None) 70 71 def forward(self, x): 72 out = self.pw2(self.se(self.dw(self.pw1(x)))) 73 if self.s == 1 and self.in_c==self.out_c: 74 out = out+x 75 return out 76 77class EfficientNetB0(nn.Module): 78 def __init__(self, n_c=3, n_classes=10): 79 super().__init__() 80 self.first = nn.Sequential( 81 nn.Conv2d(n_c,32,3,1,1,bias=False), 82 nn.BatchNorm2d(32), 83 Swish() 84 ) 85 self.mb1 = MBConv(32, 16, 3, expansion=1) 86 self.mb6_1 = nn.Sequential( 87 MBConv(16, 24, k=3, s=1, expansion=6), 88 MBConv(24, 24, k=3, s=1, expansion=6) 89 ) 90 self.mb6_2 = nn.Sequential( 91 MBConv(24, 40, k=5, s=1, expansion=6), 92 MBConv(40, 40, k=5, s=1, expansion=6) 93 ) 94 #画像サイズ:32->16 95 self.mb6_3 = nn.Sequential( 96 MBConv(40, 80, k=3, s=2, expansion=6), 97 MBConv(80, 80, k=3, s=1, expansion=6), 98 MBConv(80, 80, k=3, s=1, expansion=6), 99 ) 100 self.mb6_4 = nn.Sequential( 101 MBConv(80, 112, k=5, s=1, expansion=6), 102 MBConv(112, 112, k=5, s=1, expansion=6), 103 MBConv(112, 112, k=5, s=1, expansion=6), 104 ) 105 #画像サイズ: 16->8 106 self.mb6_5 = nn.Sequential( 107 MBConv(112, 192, k=5, s=2, expansion=6), 108 MBConv(192, 192, k=5, s=1, expansion=6), 109 MBConv(192, 192, k=5, s=1, expansion=6), 110 MBConv(192, 192, k=5, s=1, expansion=6), 111 ) 112 113 self.mb6_6 = nn.Sequential( 114 MBConv(192, 320, k=3, s=1, expansion=6), 115 ) 116 self.pw = PWBlock(320, 1280) 117 self.gap = nn.AdaptiveAvgPool2d(1) 118 self.dropout = nn.Dropout(0.2) 119 self.fc = nn.Linear(1280, n_classes) 120 121 def forward(self, x): 122 out = self.first(x) 123 out = self.mb1(out) 124 out = self.mb6_1(out) 125 out = self.mb6_2(out) 126 out = self.mb6_3(out) 127 out = self.mb6_4(out) 128 out = self.mb6_5(out) 129 out = self.mb6_6(out) 130 out = self.pw(out) 131 out = self.gap(out).view(x.size(0), -1) 132 out = self.dropout(out) 133 out = self.fc(out) 134 return out 135 136train_transform = transforms.Compose([ 137 transforms.RandomCrop(size=32, padding=4), 138 transforms.RandomHorizontalFlip(p=0.5), 139 transforms.ToTensor(), 140 transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2470, 0.2435, 0.2616]) 141]) 142train_ds = '.ora/Desktop/dataset2/train'(train=True, transform=train_transform, download=True) 143train_dl = torch.utils.data.DataLoader(train_ds, batch_size=128, num_workers=4, pin_memory=True)