def main(_): valid_data = read_data_sets(FLAGS.valid_dir, FLAGS.labellist) d = deepnn(1) graph_location = tempfile.mkdtemp( ) if FLAGS.graph_dir == None else os.path.join(FLAGS.graph_dir, str(uuid.uuid4())) log('Saving graph to: %s' % graph_location) train_writer = tf.summary.FileWriter(graph_location) train_writer.add_graph(tf.get_default_graph()) session_config = tf.ConfigProto() session_config.gpu_options.per_process_gpu_memory_fraction = FLAGS.gpu_fraction with tf.Session(config=session_config) as sess: saver = tf.train.Saver() # Load from checkpoints if FLAGS.read_from_checkpoint: ckpt = tf.train.latest_checkpoint(FLAGS.checkpoint_dir) if ckpt: saver.restore(sess, ckpt) else: sess.run(tf.global_variables_initializer()) else: sess.run(tf.global_variables_initializer()) if FLAGS.mode == "train": train_data = read_data_sets(FLAGS.data_dir, FLAGS.labellist) for i in range(FLAGS.epoch_num): inside_step = 0 while not train_data.epochs_completed: batch = train_data.next_batch(FLAGS.batch_size) # if i % (FLAGS.batch_size // 10) == 0: # train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0}) _, loss_val, summary, step, acc = sess.run( [ d['train_op'], d['loss'], d['merged_summary_op'], d['global_step'], d['accuracy'] ], feed_dict={ d['images']: batch[0].reshape([-1, 64, 64, 1]), d['labels']: batch[1], d['keep_prob']: 0.5, d['is_training']: True }) train_writer.add_summary(summary, step) if inside_step % 50 == 0: log('step %d, training accuracy %g loss is %g' % (inside_step, acc, loss_val)) inside_step += 1 train_data.restart_epoch() saver.save(sess, FLAGS.checkpoint_dir + 'model.ckpt', global_step=i + 1) valid_acc = 0.0 valid_step = 0 while not valid_data.epochs_completed: batch = valid_data.next_batch(FLAGS.batch_size) _, acc_val = sess.run( [d['logits'], d['accuracy']], feed_dict={ d['images']: batch[0].reshape([-1, 64, 64, 1]), d['labels']: batch[1], d['keep_prob']: 1.0, d['is_training']: False }) valid_acc += acc_val valid_step += 1 valid_data.restart_epoch() valid_acc = valid_acc / valid_step log('epoch %d valid accuracy %g' % (i, acc_val)) elif FLAGS.mode == "test": valid_acc = 0.0 valid_step = 0 while not valid_data.epochs_completed: batch = valid_data.next_batch(FLAGS.batch_size) labels_val, logits_val, acc_val = sess.run( [d['labels'], d['logits'], d['accuracy']], feed_dict={ d['images']: batch[0].reshape([-1, 64, 64, 1]), d['labels']: batch[1], d['keep_prob']: 1.0, d['is_training']: False }) for i in range(0, len(labels_val)): label = labels_val[i] logit = logits_val[i] input_char = int_to_chinese( data_reader.unique_label_list[label.argmax()]) output_char = int_to_chinese( data_reader.unique_label_list[logit.argmax()]) match = True if input_char == output_char else False log('Input: %s\tOuput: %s\t%r' % (input_char, output_char, match)) valid_acc += acc_val valid_step += 1 valid_acc = valid_acc / valid_step log('Valid accuracy %g' % acc_val) while True: log("Input the testing path of images or the parent directory:\n" ) sentence = sys.stdin.readline(1) test_data = read_data_sets(sentence, FLAGS.labellist)
def reset_timer(highlight): global timer_starts_at timer_starts_at = time.time() print("Timer started for tracking idle state", timer_starts_at) log_helper.log("Set timer starts at " + str(timer_starts_at), False, highlight)
def transfer_file(): seq_num = 1 fin_sent = False file_name = 'test' log_helper.init_log_file('transmitter_log.html') retransmit_count = defaultdict(int) file = open(file_name, 'rb') data = file.read(BYTES_SIZE_TO_READ) print("===============START===============") while (sobj): readable, writable, exceptional = select.select([sobj], [sobj], []) recv_packet = '' if readable: recv_packet = receive_packet() if recv_packet: recv_packet = pickle.loads(recv_packet) #Termiate the program gracefully upon receiving fin if recv_packet['ack'] == 'fin': log_helper.log( "FIN IS RETURNED FROM THE RECIEVER --- TERMINATE THE PROGRAM", False, 'pink') break if writable: if recv_packet: #Initiate sliding window by removing a packet from window when expected ACK is returned. if list(packets)[0] == recv_packet['ack']: log_helper.log( "YAY~~~ XD ACK is returned! -> " + str(recv_packet['ack']), False, '') packets.pop(recv_packet['ack']) reset_timer('Yellow') else: log_helper.log( "Expected: " + str(list(packets)[0]) + ", but received: " + str(recv_packet['ack']), True, '') #Normal behaviour upon received expected ACK while ( data and len(packets) < WINDOW_SIZE ): #Wait for ACKs from receiver when sending maximum packets if len(packets) == 0: reset_timer('Yellow') packet = construct_packet('DATA', seq_num, None, data) packets[seq_num] = packet send_packet(pickle.dumps(packet)) print("Transmit : ", seq_num, packet['data']) log_helper.log( "DATA: " + str(packet['seq']) + ";" + packet['data'].decode(), False, '') data = file.read(BYTES_SIZE_TO_READ) seq_num += 1 #On timeout, retransmit packets if len(packets) > 0 and timer_starts_at and timeout(): # if timeout for missing ack: reset_timer('pink') #Track a packet timeout. retransmit_count[list(packets)[0]] += 1 if retransmit_count[list(packets)[0]] > MAX_RETRANSMISSION: print( "Something is seriously wrong... maximum retransmission is reached... EXIT THE PROGRAM" ) log_helper.log( "Something is seriously wrong... maximum retransmission is reached... EXIT THE PROGRAM", False, 'red') # When retransmit the same packet more than maximum retransmission count.. # worthless to continue... break for seq, packet_obj in packets.items(): print("Retransmit: ", seq, packet_obj['data']) # retransmission send_packet(pickle.dumps(packet_obj)) log_helper.log( "Retransmit: " + str(packet_obj['seq']) + ";" + packet_obj['data'].decode(), False, 'grey') #No more data to send if not data: if (len(packets) == 0 and not fin_sent) or (fin_sent and timeout()): print("Transmitting FIN ACK to receiver") log_helper.log("Transmitting FIN ACK to receiver", False, '') fin_sent = True packet = construct_packet('DATA', "fin", None, None) send_packet(pickle.dumps(packet)) reset_timer('aqua') print("---------------DONE----------------") file.close() log_helper.terminate_log_file() sobj.close()
def receive_file(): sobj = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Create a UDP socket object sobj.bind(('', PORT_NUMBER)) print("Bind to ", sobj) log_helper.init_log_file('receiver_log.html') file_name = 'file_from_transmitter_when_error_rate_10' expected_seq = 1 with open(file_name, 'wb') as f: while True: recv_packet = '' readable, writable, exceptional = select.select([sobj], [sobj], []) if readable: recv_packet, address = sobj.recvfrom( 1024) # buffer size is 1024 bytes from transmitter recv_packet = pickle.loads(recv_packet) #Upon FIN ACK is received, Send FIN ACK to terminate if recv_packet['seq'] == "fin": print("Received FIN ACK from Transmitter.") packet = construct_pakcet('ACK', None, "fin", None) sobj.sendto(pickle.dumps(packet), address) log_helper.log( "FIN IS RECIVED FROM THE TRANSMITTER. SEND FIN BACK TO TRANSMITTER", False, 'pink') reset_timer('yellow') else: # Save data only if packet has a expected SEQ num if recv_packet['seq'] == expected_seq: log_helper.log( "YAY~~~ XD Expected packet is received: " + str(expected_seq), False, '') print("Save:", recv_packet['seq'], recv_packet['data']) f.write(recv_packet['data']) expected_seq = recv_packet['seq'] + 1 else: print("Discard received packet with SEQ", recv_packet['seq']) log_helper.log( "Discard received packet. Expected: " + str(expected_seq) + ", but received: " + str(recv_packet['seq']), True, '') if writable: # Always sends ACK back to transmitter if (recv_packet): print('Send ACK:', recv_packet['seq']) packet = construct_pakcet('ACK', None, recv_packet['seq'], None) sobj.sendto(pickle.dumps(packet), address) log_helper.log( "Send ACK: " + str(recv_packet['seq']) + " to transmitter", False, '') if timer_starts_at and time_to_terminate(): print( "Waited", MAXIMUM_SECONDS_IDLE, "seconds after sending final FIN ACK. -- Safe to terminate the program", time.time()) log_helper.log( "IDLE STATE AFTER FIN ACK SENT --- TERMINATE THE PROGRAM", False, 'pink') f.close() break sobj.close()
def reset_timer(highlight): global timer_starts_at timer_starts_at = time.time() log_helper.log("Set timer starts at " + str(timer_starts_at), False, highlight)