from torch.nn import functional from pathlib import Path import torchvision from torch import nn scene_detection_col = 'scene_detection' learning_rates = [0.001, 0.0001] total_loss = 0 total_correct = 0 data_folder = '/home/student/Desktop/Dan/day_night_detection' batch_sizes = [1, 10, 20] epochs = 2 num_workers = 4 train_data = get_all_with_field(scene_detection_col, field_name='train_test', field_value='train') train_ds = ImagesDataset(train_data) mark_every = len(train_ds) // 20 mark_split = [(start, start + mark_every - 1) for start in range(0, len(train_ds), mark_every)] mark_index = 0 def get_num_correct(preds, labels): return preds.argmax(dim=1).eq(labels).sum().item() def should_mark(num_processed: int) -> bool: global mark_every, mark_split, mark_index if num_processed > mark_split[mark_index][1]: mark_index += 1
if images_paths is None: images_paths = [None for _ in preds] wrong_predictions = preds.argmax(dim=1).ne(labels) return [ im_path for im_path, wrong in zip(images_paths, wrong_predictions) if wrong ] if __name__ == '__main__': import os data = get_collection_content('scene_detection') print('starting test') test_loader = DataLoader(ImagesDataset( data, lambda x: True if 'day' in x['scene'] else False), batch_size, shuffle=False, num_workers=2, pin_memory=True) detector = DayNightDetector() detector.load_state_dict(torch.load(model_path)) detector = detector.to(device='cuda') detector.eval() total = 0 wrong = 0 wrong_examples = [] for images, labels, images_paths in test_loader:
import matplotlib import torch import torchvision from torch.utils.data import DataLoader from torch.utils.tensorboard import SummaryWriter from matplotlib import pyplot from data_loader import ImagesDataset from day_night_detector import DayNightDetector from get_data import get_all_with_field from test import model_path writer = SummaryWriter('runs/scene_detection_images') val_data = get_all_with_field('scene_detection', field_name='train_test', field_value='val') val_ds = ImagesDataset(val_data) val_loader = DataLoader(val_ds, batch_size=40, shuffle=True, num_workers=0, pin_memory=True) dataiter = iter(val_loader) images, labels, _ = next(dataiter) if __name__ == '__main__': # create grid of images images.to(device='cuda') labels.to(device='cuda') detector = DayNightDetector() detector.load_state_dict(torch.load(model_path)) img_grid = torchvision.utils.make_grid(images) # show images writer.add_image('images', img_grid) writer.add_graph(detector, images) writer.close() # detector = detector.to(device='cuda')