Example #1
0
def run():
    num_classes = 13
    image_shape = (608, 800)
    data_dir = '../data'
    runs_dir = './runs'
    epochs = 10
    batch_size = 16

    if len(sys.argv) > 1:
        mode = sys.argv[1].lower()
    else:
        mode = "train"

    print("Current mode: ", mode)

    # Download pretrained vgg model
    helper.maybe_download_pretrained_vgg(data_dir)

    # OPTIONAL: Train and Inference on the cityscapes dataset instead of the Kitti dataset.
    # You'll need a GPU with at least 10 teraFLOPS to train on.
    #  https://www.cityscapes-dataset.com/

    LOGDIR = os.path.join('./data', 'fcn8_log')

    if mode == "train":

        pass

    elif mode == "test":
        if len(sys.argv) < 3:
            print("main.py test <graph location>")
        else:
            graph_file = sys.argv[2]
            use_xla = False

            config = tf.ConfigProto()
            if use_xla:
                jit_level = tf.OptimizerOptions.ON_1
                config.graph_options.optimizer_options.global_jit_level = jit_level

            with tf.Session(graph=tf.Graph(), config=config) as sess:
                gd = tf.GraphDef()
                g = sess.graph
                with tf.gfile.Open(graph_file, 'rb') as f:
                    data = f.read()
                    gd.ParseFromString(data)
                tf.import_graph_def(gd, name='')
                x = g.get_tensor_by_name('input_1:0')
                out = g.get_tensor_by_name('output_node0:0')

                t0 = time.time()
                # Save inference data using helper.save_inference_samples
                output_dir = helper.save_inference_samples_3(runs_dir, data_dir, sess, image_shape, out, None, x)
                duration = time.time() - t0
                print("Run complete, time taken = {0}".format(duration))

                helper.calculate_score(os.path.join(data_dir, 'Validate', 'CameraSeg'), output_dir)
    else:
        print("Command unrecognized.")
Example #2
0
 def read_from_s3(self):
     """
     reads files from s3 bucket defined by s3_configfile and creates Spark Dataframe
     """
     yelp_business_filename = "s3a://{}/{}/{}".format(
         self.s3_config["BUCKET"], self.s3_config["YELP_FOLDER"],
         self.s3_config["YELP_BUSINESS_DATA_FILE"])
     yelp_rating_filename = "s3a://{}/{}/{}".format(
         self.s3_config["BUCKET"], self.s3_config["YELP_FOLDER"],
         self.s3_config["YELP_REVIEW_DATA_FILE"])
     sanitary_inspection_filename = "s3a://{}/{}/{}".format(
         self.s3_config["BUCKET"], self.s3_config["INSPECTION_FOLDER"],
         self.s3_config["INSPECTION_DATA_FILE"])
     self.df_yelp_business = self.spark.read.json(yelp_business_filename)
     self.df_yelp_review = self.spark.read.json(yelp_rating_filename)
     self.df_sanitary = self.spark.read.csv(sanitary_inspection_filename,
                                            header=True)
     self.trim_zipcode_udf = udf(lambda x: helper.trim_zipcode(x),
                                 StringType())
     self.format_address_udf = udf(lambda x: helper.format_address(x),
                                   StringType())
     self.format_name_udf = udf(lambda x: helper.format_name(x),
                                StringType())
     self.fuzzy_match_udf = udf(lambda x, y: helper.fuzzy_match(x, y),
                                IntegerType())
     self.convert_sentiment_udf = udf(lambda x: helper.convert_sentiment(x),
                                      IntegerType())
     self.calculate_score_udf = udf(
         lambda x, y, z: helper.calculate_score(x, y, z), FloatType())
Example #3
0
def admin_match_all():
	ps_all = User.query.filter(User.status == "p", User.matchable == True).order_by(User.registration_date).all()

	for ps in ps_all:
		iis_all = User.query.filter(User.status == "i", User.matchable == True).all()
		best_score = None
		best_match = None

		for iis in iis_all:
			score = calculate_score(ps, iis)
			if not best_score or score > best_score:
				best_score = score
				best_match = iis

		if best_score:
			m = Matching(ps.id, best_match.id)
			db.session.add(m)
			ps.matchable = False
			best_match.matchable = False
			db.session.commit()

	return "Sucess!"
Example #4
0
def run():
    num_classes = 13
    image_shape = (608, 800)
    data_dir = '../data'
    runs_dir = './runs'
    epochs = 10
    batch_size = 16

    if len(sys.argv) > 1:
        mode = sys.argv[1].lower()
    else:
        mode = "train"

    print("Current mode: ", mode)

    # Download pretrained vgg model
    helper.maybe_download_pretrained_vgg(data_dir)

    # OPTIONAL: Train and Inference on the cityscapes dataset instead of the Kitti dataset.
    # You'll need a GPU with at least 10 teraFLOPS to train on.
    #  https://www.cityscapes-dataset.com/

    LOGDIR = os.path.join('./data', 'fcn8_log')

    if mode == "train":

        with tf.Session() as sess:
            # Path to vgg model
            vgg_path = os.path.join(data_dir, 'vgg')
            # Create function to get batches
            get_batches_fn = helper.gen_batch_function(
                os.path.join(data_dir, 'Train'), image_shape)

            # OPTIONAL: Augment Images for better results
            #  https://datascience.stackexchange.com/questions/5224/how-to-prepare-augment-images-for-neural-network

            # Build NN using load_vgg, layers, and optimize function
            input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(
                sess, vgg_path)
            layer_output = layers(layer3_out, layer4_out, layer7_out,
                                  num_classes)

            correct_label = tf.placeholder(
                tf.int32, (None, image_shape[0], image_shape[1], num_classes),
                name='correct_label')
            learning_rate = tf.placeholder(tf.float32, name='learning_rate')

            logits, train_op, cross_entropy_loss = optimize(
                layer_output, correct_label, learning_rate, num_classes)

            merged = tf.summary.merge_all()
            train_writer = tf.summary.FileWriter(LOGDIR, graph=sess.graph)

            # Train NN using the train_nn function
            train_nn(sess,
                     epochs,
                     batch_size,
                     get_batches_fn,
                     train_op,
                     cross_entropy_loss,
                     input_image,
                     correct_label,
                     keep_prob,
                     learning_rate,
                     train_writer,
                     merged,
                     logits=logits)

            # Save the model for future use
            # as_text = true will cause freeze_graph throw memory error
            tf.train.write_graph(sess.graph_def,
                                 './fcn8',
                                 'base_graph.pb',
                                 as_text=False)
            print("Model graph saved in path: ./fcn8/base_graph.pb")
            saver = tf.train.Saver()
            save_path = saver.save(sess, "./fcn8/ckpt")
            print("Model weights saved in path: %s" % save_path)

            t0 = time.time()
            # Save inference data using helper.save_inference_samples
            output_dir = helper.save_inference_samples(runs_dir, data_dir,
                                                       sess, image_shape,
                                                       logits, keep_prob,
                                                       input_image)
            duration = time.time() - t0
            print("Run complete, time taken = {0}".format(duration))

            helper.calculate_score(os.path.join(data_dir, 'Test', 'CameraSeg'),
                                   output_dir)

    elif mode == "test":
        if len(sys.argv) < 3:
            print("main.py test <graph location>")
        else:
            graph_file = sys.argv[2]
            use_xla = False

            config = tf.ConfigProto()
            if use_xla:
                jit_level = tf.OptimizerOptions.ON_1
                config.graph_options.optimizer_options.global_jit_level = jit_level

            with tf.Session(graph=tf.Graph(), config=config) as sess:
                gd = tf.GraphDef()
                g = sess.graph
                with tf.gfile.Open(graph_file, 'rb') as f:
                    data = f.read()
                    gd.ParseFromString(data)
                tf.import_graph_def(gd, name='')
                x = g.get_tensor_by_name('input_2:0')
                out = g.get_tensor_by_name('output_node0:0')

                t0 = time.time()
                # Save inference data using helper.save_inference_samples
                output_dir = helper.save_inference_samples_2(
                    runs_dir, data_dir, sess, image_shape, out, None, x)
                duration = time.time() - t0
                print("Run complete, time taken = {0}".format(duration))

                helper.calculate_score(
                    os.path.join(data_dir, 'Test', 'CameraSeg'), output_dir)
    else:
        print("Command unrecognized.")
Example #5
0
def game_flow(request):

  '''
  describes flow of bowling game and calls helper.py
  to tabulate score; keeps track of game 'states' like
  current bowling frame, pins standing, first or second roll
  args: none
  returns: scorecard with json data
  '''

  first_roll = request.session.get('first_roll')
  second_roll = request.session.get('second_roll')
  third_roll = request.session.get('third_roll')
  frame = request.session.get('frame')
  pins_standing = request.session.get('pins_standing')
  score = request.session.get('score')
  game_state = request.session.get('game_state')


  if game_state != "running":
    #Game Over.....prompt user to refresh browser
    pass
  else:  
    #continue flow of game
    if first_roll:
      pins_standing = 10

    # used later for 10th frame
    #initializes pin setup for strike scenarios
    if frame == 10:
      if second_roll and score[frame][0] == 'X':
        pins_standing = 10
      if third_roll and score[frame][1] == 'X':
        pins_standing = 10
      if third_roll and score[frame][1] == '/':
        pins_standing = 10

    #randomly hit pins on range 0 to 10
    pins_hit = randrange(0,pins_standing + 1)
    
    """
    uncomment below for unit testing
    """
    #pins_hit = int(request.GET['raw_input'])
    """
    """

    pins_standing = pins_standing - pins_hit
    request.session['pins_standing'] = pins_standing

    #game flow for first 9 frames
    if frame < 10:

      #handle strike scenario
      if first_roll and pins_standing == 0:
        score[frame][0] = "X"
        frame += 1 
        first_roll = True
        midframe = False
      #progress to second roll
      elif first_roll and pins_standing > 0:
        score[frame][0] = pins_hit
        first_roll = False
        midframe = True
      #handle spare scenario
      elif not first_roll and pins_standing == 0:
        score[frame][1] = "/"
        frame += 1
        request.session['frame'] = frame
        first_roll = True
        request.session['first_roll'] = first_roll
        midframe = False
      #end of rolls, progress to next frame
      else:
        score[frame][1] = pins_hit
        frame += 1
        first_roll = True
        midframe = False
 
      score = calculate_score(score, frame, midframe)

      '''
      game flow to handle 10th frame
      ''' 
    else:
      #first roll pins still standing
      if first_roll and pins_standing > 0:
        score[frame][0] = pins_hit
        second_roll = True
        first_roll = False
        third_roll = False
        midframe = True
      #first roll strike
      elif first_roll and pins_standing == 0:
        first_roll = False
        second_roll = True
        third_roll = False
        midframe = False
        score[frame][0] = 'X'

      #second roll scenarios
      elif second_roll and pins_standing > 0:
        #player gets bonus roll attempt for spare
        if score[frame][0] == 'X':
          first_roll = False
          second_roll = False
          third_roll = True
          midframe = True
          score[frame][1] = pins_hit
        #player does not get bonus roll
        else:
          first_roll = False
          second_roll = False
          third_roll = False
          midframe = False
          score[frame][1] = pins_hit

      # handle spare or strike scenario
      # award bonus roll
      elif second_roll and pins_standing == 0:
        if score[frame][0] == 'X':
          score[frame][1] = 'X'
          second_roll = False
          third_roll = True
          first_roll = False
          midframe = False
        else:
          score[frame][1] = '/'
          second_roll = False
          third_roll = True
          first_roll = False
          midframe = False

      #handle third roll scenarios
      elif third_roll and pins_standing > 0:
        score[frame][2] = pins_hit
        first_roll = False
        second_roll = False
        third_roll = False
        midframe = False
      elif third_roll and pins_standing == 0:
        #determine if spare or strike
        if score[frame][1] == 'X':
          score[frame][2] = 'X'
          third_roll = False
          first_roll = False
          second_roll = False
          midframe = False
        elif score[frame][1] == '/':
          score[frame][2] = 'X'
          third_roll = False
          first_roll = False
          second_roll = False
          midframe = False
        else:
          score[frame][2] ='/'
          third_roll = False
          first_roll = False
          second_roll = False
          midframe = False
   
      #update scores, check game state from helper file 
      score, game_state = calculate_10th(score, frame, first_roll, second_roll, third_roll)

    request.session['frame'] = frame
    request.session['first_roll'] = first_roll
    request.session['second_roll'] = second_roll
    request.session['third_roll'] = third_roll
    request.session['score'] = score
    request.session['game_state'] = game_state

    score['gamestate']['frame'] = frame
    score['gamestate']['pins_standing'] = pins_standing
    score['gamestate']['first_roll'] = first_roll
    score['gamestate']['game_state'] = game_state 
    score['gamestate']['midframe'] = midframe  
    score['gamestate']['second_roll'] = second_roll
    score['gamestate']['third_roll'] = third_roll    

    return HttpResponse(json.dumps(score))
Example #6
0
 def test_calculate_score(self):
     # test score calculation results
     x, y, z = 1, 1, 1
     score = 17 / 30
     self.assertItemsEqual(score, helper.calculate_score(x, y, z),
                           "errors in score calculation")