parameters: (model_path, data_dir, output_dir)
docstring: Trains a softmax regression model given data and embedding extractor.
  
    Args:
      model_path: string, path to embedding extractor.
      data_dir: string, directory that contains training data.
      output_dir: string, directory to save retrained tflite model and label map.
docstring_summary: Trains a softmax regression model given data and embedding extractor.
docstring_tokens: Trains a softmax regression model given data and embedding extractor .
function: def train(model_path, data_dir, output_dir):
    """Trains a softmax regression model given data and embedding extractor.
  
    Args:
      model_path: string, path to embedding extractor.
      data_dir: string, directory that contains training data.
      output_dir: string, directory to save retrained tflite model and label map.
    """
    t0 = time.perf_counter()
    image_paths, labels, label_map = get_image_paths(data_dir)
    train_and_val_dataset, test_dataset = shuffle_and_split(image_paths, labels)
    # Initializes basic engine model here to avoid repeatedly initialization,
    # which is time consuming.
    engine = basic_engine.BasicEngine(model_path)
    print('Extract embeddings for data_train')
    train_and_val_dataset['data_train'] = extract_embeddings(
        train_and_val_dataset['data_train'], engine)
    print('Extract embeddings for data_val')
    train_and_val_dataset['data_val'] = extract_embeddings(
        train_and_val_dataset['data_val'], engine)
    t1 = time.perf_counter()
    print('Data preprocessing takes %.2f seconds' % (t1 - t0))
  
    # Construct model and start training
    weight_scale = 5e-2
    reg = 0.0
    feature_dim = train_and_val_dataset['data_train'].shape[1]
    num_classes = np.max(train_and_val_dataset['labels_train']) + 1
    model = SoftmaxRegression(
        feature_dim, num_classes, weight_scale=weight_scale, reg=reg)
  
    learning_rate = 1e-2
    batch_size = 100
    num_iter = 500
    model.train_with_sgd(
        train_and_val_dataset, num_iter, learning_rate, batch_size=batch_size)
    t2 = time.perf_counter()
    print('Training takes %.2f seconds' % (t2 - t1))
  
    # Append learned weights to input model and save as tflite format.
    out_model_path = os.path.join(output_dir, 'retrained_model_edgetpu.tflite')
    model.save_as_tflite_model(model_path, out_model_path)
    print('Model %s saved.' % out_model_path)
    label_map_path = os.path.join(output_dir, 'label_map.txt')
    save_label_map(label_map, label_map_path)
    print('Label map %s saved.' % label_map_path)
    t3 = time.perf_counter()
    print('Saving retrained model and label map takes %.2f seconds' % (t3 - t2))
  
    retrained_engine = basic_engine.BasicEngine(out_model_path)
    test_embeddings = extract_embeddings(test_dataset['data_test'],
                                         retrained_engine)
    saved_model_acc = np.mean(
        np.argmax(test_embeddings, axis=1) == test_dataset['labels_test'])
    print('Saved tflite model test accuracy: %.2f%%' % (saved_model_acc * 100))
    t4 = time.perf_counter()
    print('Checking test accuracy takes %.2f seconds' % (t4 - t3))
function_tokens: def train ( model_path , data_dir , output_dir ) : t0 = time . perf_counter ( ) image_paths , labels , label_map = get_image_paths ( data_dir ) train_and_val_dataset , test_dataset = shuffle_and_split ( image_paths , labels ) # Initializes basic engine model here to avoid repeatedly initialization, # which is time consuming. engine = basic_engine . BasicEngine ( model_path ) print ( 'Extract embeddings for data_train' ) train_and_val_dataset [ 'data_train' ] = extract_embeddings ( train_and_val_dataset [ 'data_train' ] , engine ) print ( 'Extract embeddings for data_val' ) train_and_val_dataset [ 'data_val' ] = extract_embeddings ( train_and_val_dataset [ 'data_val' ] , engine ) t1 = time . perf_counter ( ) print ( 'Data preprocessing takes %.2f seconds' % ( t1 - t0 ) ) # Construct model and start training weight_scale = 5e-2 reg = 0.0 feature_dim = train_and_val_dataset [ 'data_train' ] . shape [ 1 ] num_classes = np . max ( train_and_val_dataset [ 'labels_train' ] ) + 1 model = SoftmaxRegression ( feature_dim , num_classes , weight_scale = weight_scale , reg = reg ) learning_rate = 1e-2 batch_size = 100 num_iter = 500 model . train_with_sgd ( train_and_val_dataset , num_iter , learning_rate , batch_size = batch_size ) t2 = time . perf_counter ( ) print ( 'Training takes %.2f seconds' % ( t2 - t1 ) ) # Append learned weights to input model and save as tflite format. out_model_path = os . path . join ( output_dir , 'retrained_model_edgetpu.tflite' ) model . save_as_tflite_model ( model_path , out_model_path ) print ( 'Model %s saved.' % out_model_path ) label_map_path = os . path . join ( output_dir , 'label_map.txt' ) save_label_map ( label_map , label_map_path ) print ( 'Label map %s saved.' % label_map_path ) t3 = time . perf_counter ( ) print ( 'Saving retrained model and label map takes %.2f seconds' % ( t3 - t2 ) ) retrained_engine = basic_engine . BasicEngine ( out_model_path ) test_embeddings = extract_embeddings ( test_dataset [ 'data_test' ] , retrained_engine ) saved_model_acc = np . mean ( np . argmax ( test_embeddings , axis = 1 ) == test_dataset [ 'labels_test' ] ) print ( 'Saved tflite model test accuracy: %.2f%%' % ( saved_model_acc * 100 ) ) t4 = time . perf_counter ( ) print ( 'Checking test accuracy takes %.2f seconds' % ( t4 - t3 ) )