<jupyter_start><jupyter_text>유틸리티 함수<jupyter_code>def imshow(inp,cmap=None):
    """Imshow for Tensor."""
    inp = inp.numpy().transpose((1, 2, 0))
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    inp = std * inp + mean
    inp = np.clip(inp, 0, 1)
    plt.imshow(inp,cmap)
    
class FeaturesDataset(Dataset):
    
    def __init__(self,featlst,labellst):
        self.featlst = featlst
        self.labellst = labellst
        
    def __getitem__(self,index):
        return (self.featlst[index],self.labellst[index])
    
    def __len__(self):
        return len(self.labellst)
    
def fit(epoch,model,data_loader,phase='training',volatile=False):
    if phase == 'training':
        model.train()
    if phase == 'validation':
        model.eval()
        volatile=True
    running_loss = 0.0
    running_correct = 0
    for batch_idx , (data,target) in enumerate(data_loader):
        if is_cuda:
            data,target = data.cuda(),target.cuda()
        data , target = Variable(data,volatile),Variable(target)
        if phase == 'training':
            optimizer.zero_grad()
        output = model(data)
        loss = F.cross_entropy(output,target)
        
        running_loss += F.cross_entropy(output,target,size_average=False).data
        preds = output.data.max(dim=1,keepdim=True)[1]
        running_correct += preds.eq(target.data.view_as(preds)).cpu().sum()
        if phase == 'training':
            loss.backward()
            optimizer.step()
    
    loss = running_loss/len(data_loader.dataset)
    accuracy = 100. * running_correct.item()/len(data_loader.dataset)
    
    print(f'{phase} loss is {loss:{5}.{2}} and {phase} accuracy is {running_correct}/{len(data_loader.dataset)}{accuracy:{10}.{4}}')
    return loss,accuracy<jupyter_output><empty_output><jupyter_text>PyTorch 데이터셋 생성<jupyter_code>data_transform = transforms.Compose([
        transforms.Resize((299,299)),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])

# For Dogs & Cats dataset
train_dset = ImageFolder('../Chapter03/dogsandcats/train/',transform=data_transform)
val_dset = ImageFolder('../Chapter03/dogsandcats/valid/',transform=data_transform)
classes=2
imshow(train_dset[150][0])<jupyter_output><empty_output><jupyter_text>학습과 검증 데이터셋을 위한 데이터 로더 생성<jupyter_code>train_loader = DataLoader(train_dset,batch_size=32,shuffle=False,num_workers=3)
val_loader = DataLoader(val_dset,batch_size=32,shuffle=False,num_workers=3)<jupyter_output><empty_output><jupyter_text>ResNet 34 모델 생성<jupyter_code>my_resnet = resnet34(pretrained=True)

if is_cuda:
    my_resnet = my_resnet.cuda()
m = nn.Sequential(*list(my_resnet.children())[:-1])<jupyter_output><empty_output><jupyter_text>컨볼루션 피처 추출<jupyter_code>#For training data

# Stores the labels of the train data
trn_labels = [] 

# Stores the pre convoluted features of the train data
trn_features = [] 

#Iterate through the train data and store the calculated features and the labels
for d,la in train_loader:
    o = m(Variable(d.cuda()))
    o = o.view(o.size(0),-1)
    trn_labels.extend(la)
    trn_features.extend(o.cpu().data)

#For validation data

#Iterate through the validation data and store the calculated features and the labels
val_labels = []
val_features = []
for d,la in val_loader:
    o = m(Variable(d.cuda()))
    o = o.view(o.size(0),-1)
    val_labels.extend(la)
    val_features.extend(o.cpu().data)<jupyter_output><empty_output><jupyter_text>학습과 검증 피처 데이터셋 생성<jupyter_code>#Creating dataset for train and validation
trn_feat_dset = FeaturesDataset(trn_features,trn_labels)
val_feat_dset = FeaturesDataset(val_features,val_labels)

#Creating data loader for train and validation
trn_feat_loader = DataLoader(trn_feat_dset,batch_size=64,shuffle=True)
val_feat_loader = DataLoader(val_feat_dset,batch_size=64)<jupyter_output><empty_output><jupyter_text>전연결 네트워크<jupyter_code>class FullyConnectedModel(nn.Module):
    
    def __init__(self,in_size,out_size):
        super().__init__()
        self.fc = nn.Linear(in_size,out_size)

    def forward(self,inp):
        out = self.fc(inp)
        return out
fc_in_size = 8192
fc = FullyConnectedModel(fc_in_size,classes)
if is_cuda:
    fc = fc.cuda()
optimizer = optim.Adam(fc.parameters(),lr=0.0001)<jupyter_output><empty_output><jupyter_text>학습과 검증 모델<jupyter_code>train_losses , train_accuracy = [],[]
val_losses , val_accuracy = [],[]
for epoch in range(1,10):
    epoch_loss, epoch_accuracy = fit(epoch,fc,trn_feat_loader,phase='training')
    val_epoch_loss , val_epoch_accuracy = fit(epoch,fc,val_feat_loader,phase='validation')
    train_losses.append(epoch_loss)
    train_accuracy.append(epoch_accuracy)
    val_losses.append(val_epoch_loss)
    val_accuracy.append(val_epoch_accuracy)
for epoch in range(1,10):
    epoch_loss, epoch_accuracy = fit(epoch,fc,trn_feat_loader,phase='training')
    val_epoch_loss , val_epoch_accuracy = fit(epoch,fc,val_feat_loader,phase='validation')
    train_losses.append(epoch_loss)
    train_accuracy.append(epoch_accuracy)
    val_losses.append(val_epoch_loss)
    val_accuracy.append(val_epoch_accuracy)
for epoch in range(1,10):
    epoch_loss, epoch_accuracy = fit(epoch,fc,trn_feat_loader,phase='training')
    val_epoch_loss , val_epoch_accuracy = fit(epoch,fc,val_feat_loader,phase='validation')
    train_losses.append(epoch_loss)
    train_accuracy.append(epoch_accuracy)
    val_losses.append(val_epoch_loss)
    val_accuracy.append(val_epoch_accuracy)
for epoch in range(1,10):
    epoch_loss, epoch_accuracy = fit(epoch,fc,trn_feat_loader,phase='training')
    val_epoch_loss , val_epoch_accuracy = fit(epoch,fc,val_feat_loader,phase='validation')
    train_losses.append(epoch_loss)
    train_accuracy.append(epoch_accuracy)
    val_losses.append(val_epoch_loss)
    val_accuracy.append(val_epoch_accuracy)<jupyter_output>training loss is 0.081 and training accuracy is 22512/23000     97.88
validation loss is   0.1 and validation accuracy is 1929/2000     96.45
training loss is  0.08 and training accuracy is 22500/23000     97.83
validation loss is 0.097 and validation accuracy is 1934/2000      96.7
training loss is 0.077 and training accuracy is 22520/23000     97.91
validation loss is 0.097 and validation accuracy is 1932/2000      96.6
training loss is 0.076 and training accuracy is 22529/23000     97.95
validation loss is 0.095 and validation accuracy is 1933/2000     96.65
training loss is 0.073 and training accuracy is 22544/23000     98.02
validation loss is 0.093 and validation accuracy is 1934/2000      96.7
training loss is 0.074 and training accuracy is 22537/23000     97.99
validation loss is 0.093 and validation accuracy is 1936/2000      96.8
training loss is 0.069 and training accuracy is 22602/23000     98.27
validation loss is 0.094 and validation accuracy is 1939/2000     96.95
traini[...]
{'fraction_non_alphanumeric': 0.11287988422575977, 'fraction_numerical': 0.050940665701881334, 'mean_word_length': 4.36985236985237, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '82391', 'license': 'MIT', 'n_tokens_mistral': 2702, 'n_tokens_neox': 2567, 'n_words': 466, 'path': 'Chapter08/DogsandCatsUsingResnet_completed.ipynb', 'repo_name': 'taewanme/DL_With_PyTorch', 'size': '6910'}