def __init__(self, opts): for key, value in opts.items(): setattr(self, key, value) try: makedirs(self.training_results_path) except: pass # datasets and loaders self.train_loader = get_loader(self, 'train', drop_last=True) self.test_loader = get_loader(self, 'test', drop_last=False) # model self.model = Network().construct(self.net, self) self.model.to(self.device) # loss func = getattr(nn, self.crit) self.criterion = func() # optimizer and learning rate schedualer func = getattr(optim, self.optim) self.optimizer = func(self.model.parameters(), lr=self.lr, **self.optim_kwargs) self.lr_scheduler = MultiStepLR(self.optimizer, milestones=self.milestones, gamma=self.gamma)
def __init__(self, num_classes): Network.__init__(self) self._losses = {} self._predictions = {} self._event_summaries = {} self._act_summaries = [] self._score_summaries = {} self._train_summaries = [] self._num_classes = num_classes self._num_batch = 256
def __init__(self, num_classes, width=32, convnet=Convnet_8): Network.__init__(self) self._width = width self._losses = {} self._predictions = {} self._event_summaries = {} self._act_summaries = [] self._score_summaries = {} self._train_summaries = [] self._num_classes = num_classes self._convnet = convnet
def __init__(self): # register signal handler signal.signal(signal.SIGINT, self._signal_handler) signal.signal(signal.SIGTERM, self._signal_handler) # flag for activity before quit self.is_running = True self.is_sig = False # user's attributes self.nick = None self.ip = None self.port = None # create Hnefatafl self.hnef = Hnefatafl() # create client network self.net = Network(self) # create gui self.gui = Gui(self)
import random FTRAIN = '/home/mihael/Documents/9. semestar/VIROKR/Projekt/Detecting-Facial-Features-CNN/dataset/kaggle/training.csv' # dataset specs X_train, y_train = load_dataset_spplited(fname=FTRAIN, test=False) print("splitted=", X_train.shape, y_train.shape) pic_width = 96 pic_height = 96 pic_channels = 1 # grayscale num_classes = 15 * 2 # other input = PicturePlaceholder( sample_input_shape=[pic_height, pic_width, pic_channels]) output = LabelsPlaceholder(num_classes=num_classes) cnn = Network() # First CNN layer cnn.add_layer(BatchNormLayer(name="batch_norm1"))\ .add_layer(ConvolutionalLayer(name="conv1", filter_size=5, num_filters=24, strides=[1, 1, 1, 1])) \ .add_layer(ActivationLayer(name="relu1", activation_fn=tf.nn.relu))\ .add_layer(MaxPoolLayer(name="pool1", padding="VALID")) # Second CNN layer cnn.add_layer(ConvolutionalLayer(name="conv2", filter_size=5, num_filters=36, strides=[1, 1, 1, 1], padding="VALID")) \ .add_layer(ActivationLayer(name="relu2", activation_fn=tf.nn.relu))\ .add_layer(MaxPoolLayer(name="pool2", padding="VALID")) # Third CNN layer cnn.add_layer(ConvolutionalLayer(name="conv3", filter_size=5, num_filters=48, strides=[1, 1, 1, 1], padding="VALID")) \ .add_layer(ActivationLayer(name="relu3", activation_fn=tf.nn.relu))\ .add_layer(MaxPoolLayer(name="pool3", padding="VALID"))
class Experiment: def __init__(self, opts): for key, value in opts.items(): setattr(self, key, value) try: makedirs(self.training_results_path) except: pass # datasets and loaders self.train_loader = get_loader(self, 'train', drop_last=True) self.test_loader = get_loader(self, 'test', drop_last=False) # model self.model = Network().construct(self.net, self) self.model.to(self.device) # loss func = getattr(nn, self.crit) self.criterion = func() # optimizer and learning rate schedualer func = getattr(optim, self.optim) self.optimizer = func(self.model.parameters(), lr=self.lr, **self.optim_kwargs) self.lr_scheduler = MultiStepLR(self.optimizer, milestones=self.milestones, gamma=self.gamma) def run(self, stats_meter, stats_no_meter): # seed random.seed(self.seed) torch.manual_seed(self.seed) torch.cuda.manual_seed_all(self.seed) # starts at the last epoch for epoch in range(1, self.epochs + 1): # adjust learning rate if self.lr_scheduler: self.lr_scheduler.step() # json dump file results_src_old = self.training_results_path + '/results_epoch=' + str( epoch - 1) results_src = self.training_results_path + '/results_epoch=' + str( epoch) results = DumpJSON(read_path=(results_src_old + '.json'), write_path=(results_src + '.json')) # train results = self.run_epoch("train", epoch, self.train_loader, stats_meter, stats_no_meter, results) # test results = self.run_epoch("test", epoch, self.test_loader, stats_meter, stats_no_meter, results) # dump to json results.save() results.to_csv() def run_epoch(self, phase, epoch, loader, stats_meter, stats_no_meter, results): # average meters meters = {} for name, func in stats_meter.items(): meters[name] = AverageMeter() # switch phase if phase == 'train': self.model.train() elif phase == 'test': self.model.eval() else: raise Exception('Phase must be train, test or analysis!') for iter, batch in enumerate(loader, 1): # input and target input = batch[0] target = batch[1] if not isinstance(target, torch.LongTensor): target = target.view(input.shape[0], -1).type(torch.LongTensor) input = input.to(self.device) target = target.to(self.device) # run model on input and compare estimated result to target est = self.model(input) loss = self.criterion(est, target) # compute gradient and do optimizer step if phase == 'train': self.optimizer.zero_grad() loss.backward() self.optimizer.step() # record statistics for name, func in stats_meter.items(): meters[name].update(func(locals()), input.data.shape[0]) # print statistics output = '{}\t' \ 'Network: {}\t' \ 'Dataset: {}\t' \ 'Epoch: [{}/{}][{}/{}]\t' \ .format(phase.capitalize(), self.net, self.dataset, epoch, self.epochs, iter, len(loader)) for name, meter in meters.items(): output = output + '{}: {meter.val:.4f} ({meter.avg:.4f})\t' \ .format(name, meter=meter) print(output) sys.stdout.flush() # append row to results CSV file if results is not None: if iter == len(loader): stats = { 'phase': phase, 'dataset': self.dataset, 'epoch': epoch, 'iter': iter, 'iters': len(loader) } for name, meter in meters.items(): stats['iter_' + name] = meter.val stats['avg_' + name] = meter.avg for name, func in stats_no_meter.items(): stats[name] = func(locals()) results.append(dict(self.__getstate__(), **stats)) return results def __getstate__(self): state = self.__dict__.copy() # remove fields that should not be saved attributes = [ 'train_transform', 'test_transform', 'train_loader', 'test_loader', 'model', 'criterion', 'optimizer', 'lr_scheduler', 'device', ] for attr in attributes: try: del state[attr] except: pass return state
from placeholders.lr.iterative_lr import IterativeLearningRate from placeholders.picture_placeholder import PicturePlaceholder import argparse import sys # dataset specs pic_width = 96 pic_height = 96 pic_channels = 1 # grayscale num_classes = 15 * 2 # other input = PicturePlaceholder( sample_input_shape=[pic_height, pic_width, pic_channels]) output = LabelsPlaceholder(num_classes=num_classes) cnn = Network() # First CNN layer cnn.add_layer(BatchNormLayer(name="batch_norm1")) \ .add_layer(ConvolutionalLayer(name="conv1", filter_size=5, num_filters=24, strides=[1, 1, 1, 1])) \ .add_layer(ActivationLayer(name="relu1", activation_fn=tf.nn.relu)) \ .add_layer(MaxPoolLayer(name="pool1", padding="VALID")) # Second CNN layer cnn.add_layer(ConvolutionalLayer(name="conv2", filter_size=5, num_filters=36, strides=[1, 1, 1, 1], padding="VALID")) \ .add_layer(ActivationLayer(name="relu2", activation_fn=tf.nn.relu)) \ .add_layer(MaxPoolLayer(name="pool2", padding="VALID")) # Third CNN layer cnn.add_layer(ConvolutionalLayer(name="conv3", filter_size=5, num_filters=48, strides=[1, 1, 1, 1], padding="VALID")) \ .add_layer(ActivationLayer(name="relu3", activation_fn=tf.nn.relu)) \ .add_layer(MaxPoolLayer(name="pool3", padding="VALID"))
class Application: def __init__(self): # register signal handler signal.signal(signal.SIGINT, self._signal_handler) signal.signal(signal.SIGTERM, self._signal_handler) # flag for activity before quit self.is_running = True self.is_sig = False # user's attributes self.nick = None self.ip = None self.port = None # create Hnefatafl self.hnef = Hnefatafl() # create client network self.net = Network(self) # create gui self.gui = Gui(self) def _signal_handler(self, sig, frame): self.is_running = False self.is_sig = True self.gui.destroy() logger.info("Closing client with signal.") def run(self): # start network self.net.start() # start the application -- has to be last command, # because it runs, until the window is closed == everything else freezes self.gui.mainloop() # if windows was closed with button or cross, set flag in standard way self.is_running = False if not self.is_sig: logger.info("Closing client standard way.") # notify network thread, in order to end it with self.net.cv: self.net.cv.notify() # join Network thread self.net.join() # print statistics through lifetime self.net.pr_statistics() def hnef_connect(self, nick, ip, port): self.nick = nick self.ip = "127.0.0.1" if ip == "localhost" else ip self.port = int(port) # connect to server self.net.connect(self.nick, self.ip, self.port) def gui_connected(self): self.gui.make_connected() def gui_disconnected(self): self.gui.make_disconnected() def send_to_server(self, code, value=None): self.net.send_msg(code, value) def send_to_chat(self, msg, bot): self.gui.chat_msg_server(msg, bot, self.hnef.nick_opponent) def send_to_menu(self, msg): self.gui.set_state(msg) def is_in_game(self): # True, if somebody is on turn -- means game is on return self.hnef.on_turn is not None def start_game(self, turn, opn_name): # start new game in Hnefatafl class self.hnef.new_game(turn, opn_name) # switch frame in GUI, so user can play self.gui.new_game(self.nick, self.hnef.game_state, self.hnef.pf, self.hnef.allowed_squares) # tell player, who is on turn self.send_to_chat("'{}' is black and on turn.".format(self.nick if turn else opn_name), bot=True) def leave_game(self): self.send_to_server(protocol.CC_LEAV) self.hnef.quit_game() def reset_game(self, turn, nick_opn, pf): # reset game in Hnefatafl class self.hnef.reset_game(turn, nick_opn, pf) # not actually new game -- it has state like in received message from server self.gui.new_game(self.nick, self.hnef.game_state, self.hnef.pf, self.hnef.allowed_squares) def quit_game(self, result): # quit game after game-over self.hnef.quit_game() self.gui.quit_game(result) def handle_click(self, x_pos, y_pos): redraw = True # playfield have not been clicked if self.hnef.game_state == Click.THINKING: # find fields where player may move after click on stone self.hnef.find_placeable_fields(x_pos, y_pos) # set position, which is being moved from self.hnef.x_from = x_pos self.hnef.y_from = y_pos # change state to clicked self.hnef.game_state = Click.CLICKED # playfield have been clicked elif self.hnef.game_state == Click.CLICKED: # field without stone have been clicked -- make a move if self.hnef.is_field(x_pos, y_pos): # send move message to server move = self.compose_move_msg([self.hnef.x_from, self.hnef.y_from, x_pos, y_pos]) self.send_to_server(protocol.CC_MOVE, value=move) # save also position which is being moved to and move after server's confirmation of valid move self.hnef.x_to = x_pos self.hnef.y_to = y_pos # playfield is updated and redrawn after server's MOVE_VALID message redraw = False # field with same stone have been clicked -- go back to thinking state elif self.hnef.is_same_stone(x_pos, y_pos): # find stones, which player can move with self.hnef.find_movables_stones() # reset position, which is being moved from self.hnef.x_from = None self.hnef.y_from = None # change state to thinking self.hnef.game_state = Click.THINKING # field with other stone have been clicked -- find allowed squares again else: # find stones, which player can move with self.hnef.find_movables_stones() # find fields where player may move after click on stone self.hnef.find_placeable_fields(x_pos, y_pos) # set position, which is being moved from self.hnef.x_from = x_pos self.hnef.y_from = y_pos # update playfield in gui if redraw: self.gui.pf_update(self.hnef.game_state, self.hnef.pf, self.hnef.allowed_squares) def move_self(self): # move chosen stone (after server confirmation -- so use cached values) self.hnef.move(self.hnef.x_from, self.hnef.y_from, self.hnef.x_to, self.hnef.y_to) # check captures of local player -- capturing white if local is black self.hnef.check_captures(self.hnef.is_surrounded_white if self.hnef.black else self.hnef.is_surrounded_black) # after move, player cannot move anything self.hnef.allowed_squares.clear() # update playfield in gui self.gui.pf_update(self.hnef.game_state, self.hnef.pf, self.hnef.allowed_squares) def move_opponent(self, x_from, y_from, x_to, y_to): # move opponent's piece self.hnef.move(x_from, y_from, x_to, y_to) # check captures of opponent player -- capturing black if local is black self.hnef.check_captures(self.hnef.is_surrounded_black if self.hnef.black else self.hnef.is_surrounded_white) # get pieces of player, who is on turn now self.hnef.find_movables_stones() # update playfield in gui self.gui.pf_update(self.hnef.game_state, self.hnef.pf, self.hnef.allowed_squares) def compose_move_msg(self, coordinates): move = "" for coor in coordinates: # if number has only one place, so append 0 at beginning according to protocol # else it is 10 with two places move += "0" + str(coor) if coor < 10 else str(coor) return move
tf.set_random_seed(100) random.seed(100) # dataset specs X_train, y_train = load_dataset_spplited(fname=FTRAIN, test=False) print("splitted=", X_train.shape, y_train.shape) pic_width = 96 pic_height = 96 pic_channels = 1 # grayscale num_classes = 15 * 2 # other input = PicturePlaceholder( sample_input_shape=[pic_height, pic_width, pic_channels]) output = LabelsPlaceholder(num_classes=num_classes) cnn = Network() # First CNN layer cnn.add_layer(ConvolutionalLayer(name="conv1", filter_size=5, num_filters=24))\ .add_layer(MaxPoolLayer(name="pool1"))\ .add_layer(BatchNormLayer(name="batch_norm1"))\ .add_layer(ActivationLayer(name="relu1", activation_fn=tf.nn.relu)) # Second CNN layer cnn.add_layer(ConvolutionalLayer(name="conv2", filter_size=5, num_filters=36))\ .add_layer(MaxPoolLayer(name="pool2"))\ .add_layer(BatchNormLayer(name="batch2"))\ .add_layer(ActivationLayer(name="relu2", activation_fn=tf.nn.relu)) # Third CNN layer cnn.add_layer(ConvolutionalLayer(name="conv3", filter_size=5, num_filters=48))\ .add_layer(MaxPoolLayer(name="pool3"))\