Exemplo n.º 1
0
def load_model(filepath=None, config=None, item=None):
    """
    Load pre-trained model
    Support the model that created by sklearn.externals.joblib

    Parameters
    ----------
    filepath : string, default None
        The model file name

    Returns
    -------
    The model created by sklearn
    """

    if filepath is None:
        raise ValueError("The filepath is None, please check the filepath is in the config file")
    if '.h5' in filepath:
        keras_model = lm(filepath)
        reader = FeatureReader(config)
        features = reader.get_feature(dt.now())
        f = features[item]
        # for keras bug
        v = keras_model.predict(f)
        return keras_model
    else:
        return joblib.load(filepath)
    def __init__(self, carla_client, args):
        self.client = carla_client
        self._carla_settings = make_carla_settings(args)
        self._timer = None
        self._display = None
        self._main_image = None
        self._mini_view_image1 = None
        self._mini_view_image2 = None
        self._enable_autopilot = args.autopilot
        self._enable_imitation = args.imitation
        self._lidar_measurement = None
        self._map_view = None
        self._is_on_reverse = False
        self._display_map = args.map
        self._city_name = None
        self._map = None
        self._map_shape = None
        self._map_view = None
        self._position = None
        self._agent_positions = None
        self.model = lm('./model_semseg.hdf5')

        # variables to enable saving to disk
        self.out_filename_format = args.out_filename_format
        self.save_images_to_disk = args.save_images_to_disk
        self.episode_count = 0
        self.frame_count = 0
        self.saver = None
        self.out_dir = args.location
Exemplo n.º 3
0
def init():
    print("Loading model...")
    model = lm(getModelPath())
    print("Model loaded successfully.")
    graph = tf.get_default_graph()

    return graph, model
Exemplo n.º 4
0
def learn_from_memory(model, is_lstm=False, clone_model=None):
    """ Use replay memory to learn. Ignore s2 if s1 is terminal """

    s1 = None
    if is_lstm == True:
        s1, a, s2, isterminal, r = memory.get_last_entry()
    else:
        if memory.size > batch_size:
            s1, a, s2, isterminal, r = memory.get_sample(batch_size)

    # lstm predict updates the state of the lstm modules
    #get the current state action values. LET the lstm state get updated,
    # BUT save the weights for fitting
    model.save(tmp_model_savefile)
    target_q = model.predict(
        s1, batch_size=batch_size
    )  #lstm predict updates the state of the lstm modules

    #lstm predict updates the state of the lstm modules
    #get next state lstm values, but recover the lstm state using the clone model to hold weights

    q_next = model.predict(
        s2, batch_size=batch_size
    )  #lstm predict updates the state of the lstm modules
    max_q_next = np.max(q_next, axis=1)
    target_q[np.arange(target_q.shape[0]),
             a] = r + discount_factor * (1 - isterminal) * max_q_next
    #now recover the weights (including the memory) for fitting
    model = lm(tmp_model_savefile)
    model.fit(s1, target_q, batch_size=batch_size, verbose=0)

    #todo test if fit changed the prediction

    #AND again step the lstm forward for the next state.
    _ = model.predict(s1, batch_size=batch_size)
Exemplo n.º 5
0
    def trainSingle(self, train, test):

        # writing targets in keras readable shape
        best = str(int(time.time()))
        y_train = to_categorical(train["target"].values)
        y_test = to_categorical(test["target"].values)

        N_classes = len(y_train[0])

        model_impl = getattr(keras_models, self.params["name"])
        model = model_impl(len(self.variables), N_classes)
        model.summary()
        model.fit(
            train[self.variables].values,
            y_train,
            sample_weight=train["train_weight"].values,
            # validation_split = 0.25,
            validation_data=(test[self.variables].values, y_test,
                             test["train_weight"].values),
            batch_size=self.params["batch_size"],
            epochs=self.params["epochs"],
            shuffle=False,
            callbacks=[
                EarlyStopping(patience=self.params["early_stopping"]),
                ModelCheckpoint(best + ".model",
                                save_best_only=True,
                                verbose=1)
            ])

        print "Reloading best model"
        model = lm(best + ".model")
        os.remove(best + ".model")

        return model
Exemplo n.º 6
0
def load_model(filepath=None):
    """
    Load pre-trained model
    Support the model that created by sklearn.externals.joblib

    Parameters
    ----------
    filepath : string, default None
        The model file name

    Returns
    -------
    The model created by sklearn, xgboost or keras, and the model input shape
    """

    if filepath is None:
        raise ValueError("The filepath is None, please check the filepath is in the config file")
    if '.h5' in filepath:
        keras_model = lm(filepath)
        feature_shape = keras_model.layers[0].input_shape
        array_shape = [1] + [i for i in feature_shape[1:]]
        array = np.zeros(array_shape)
        # for keras bug
        keras_model.predict(array)
        return keras_model, feature_shape
    else:
        return joblib.load(filepath), None
Exemplo n.º 7
0
def main(argv):
    # builds model

    curmodel = lm(argv[0])
    datafile = argv[1]
    tone = errors(curmodel, datafile)
    print("Correct Prediciton: ", tone[0])
    print("Missed to Right: ", tone[1])
    print("Missed to Left: ", tone[2])
    print("Standard Error: ", tone[3])
    print("Squared Error: ", tone[4])
Exemplo n.º 8
0
    def trainSingle(self, train, test):

        # writing targets in keras readable shape
        best = str(int(time.time()))
        y_train = to_categorical(train["target"].values)
        y_test = to_categorical(test["target"].values)

        N_classes = len(y_train[0])

        model_impl = getattr(
            keras_models,
            self.params["name"])  # reads model defined in conf/keras_models.py
        model = (len(self.variables), N_classes)
        model.summary()
        history = model.fit(
            train[self.variables].values,
            y_train,
            sample_weight=train["train_weight"].values,
            validation_split=0.25,
            # validation_data=(test[self.variables].values, y_test, test["train_weight"].values),
            batch_size=self.params["batch_size"],
            epochs=self.params["epochs"],
            shuffle=True,
            callbacks=[
                EarlyStopping(patience=self.params["early_stopping"]),
                ModelCheckpoint(best + ".model",
                                save_best_only=True,
                                verbose=1)
            ])

        import matplotlib as mpl
        mpl.use('Agg')
        import matplotlib.pyplot as plt

        print "plotting training"
        epochs = xrange(1, len(history.history["loss"]) + 1)
        plt.plot(epochs, history.history["loss"], lw=3, label="Training loss")
        plt.plot(epochs,
                 history.history["val_loss"],
                 lw=3,
                 label="Validation loss")
        plt.xlabel("Epoch")
        plt.ylabel("Loss")
        plt.legend()
        if not os.path.exists("plots"):
            os.mkdir("plots")
        plt.savefig("plots/fold_{0}_loss.png".format(best),
                    bbox_inches="tight")

        print "Reloading best model"
        model = lm(best + ".model")
        os.remove(best + ".model")

        return model
Exemplo n.º 9
0
    def load(self, filename):
        with open(filename + ".dict", 'rb') as FSO:
            tmp_dict = json.load(FSO)

        print "Loading model from: " + filename
        self.__dict__.clear()
        self.__dict__.update(tmp_dict)

        self.models = []
        for model in tmp_dict["models"]:
            self.models.append(lm(model))
def make_carla_settings(args):
    """Make a CarlaSettings object with the settings we need."""
    model = lm('./model_semseg.hdf5')
    settings = CarlaSettings()
    settings.set(SynchronousMode=True,
                 SendNonPlayerAgentsInfo=True,
                 NumberOfVehicles=5,
                 NumberOfPedestrians=30,
                 WeatherId=random.choice(list(range(15))),
                 QualityLevel=args.quality_level)
    settings.randomize_seeds()
    camera0 = sensor.Camera('CameraRGB')
    camera0.set_image_size(WINDOW_WIDTH, WINDOW_HEIGHT)
    camera0.set_position(2.0, 0.0, 1.4)
    camera0.set_rotation(0.0, 0.0, 0.0)
    settings.add_sensor(camera0)
    # camera1 = sensor.Camera('CameraDepth', PostProcessing='Depth')
    # # camera1.set_image_size(MINI_WINDOW_WIDTH, MINI_WINDOW_HEIGHT)
    # camera1.set_image_size(WINDOW_WIDTH, WINDOW_HEIGHT)
    # camera1.set_position(2.0, 0.0, 1.4)
    # camera1.set_rotation(0.0, 0.0, 0.0)
    # settings.add_sensor(camera1)
    camera2 = sensor.Camera('CameraSemSeg',
                            PostProcessing='SemanticSegmentation')
    # camera2.set_image_size(MINI_WINDOW_WIDTH, MINI_WINDOW_HEIGHT)
    camera2.set_image_size(WINDOW_WIDTH, WINDOW_HEIGHT)
    camera2.set_position(2.0, 0.0, 1.4)
    camera2.set_rotation(0.0, 0.0, 0.0)
    settings.add_sensor(camera2)
    # if args.lidar:
    #     lidar = sensor.Lidar('Lidar32')
    #     lidar.set_position(0, 0, 2.5)
    #     lidar.set_rotation(0, 0, 0)
    #     lidar.set(
    #         Channels=32,
    #         Range=50,
    #         PointsPerSecond=100000,
    #         RotationFrequency=10,
    #         UpperFovLimit=10,
    #         LowerFovLimit=-30)
    #     settings.add_sensor(lidar)
    return settings
Exemplo n.º 11
0
    def load(self, filename):
        with open(filename + ".dict", 'rb') as FSO:
            tmp_dict = json.load(FSO)

        logger.debug( "Dict located in: " + filename)
        self.__dict__.clear()
        self.__dict__.update(tmp_dict)

        self.models = []
        for modelpath in tmp_dict["models"]:
            logger.debug("Filename is " + filename)
            logger.debug("Path in dict is " + modelpath)
            modelname = os.path.basename(modelpath)

            dirpath = os.path.dirname(filename)
            actual_modelpath = os.path.join(dirpath, modelname)

            logger.debug("Loading model from: " + actual_modelpath)

            self.models.append(lm(actual_modelpath))
 def __init__(self, carla_client, args):
     self.client = carla_client
     self._carla_settings = make_carla_settings(args)
     self._timer = None
     self._display = None
     self._main_image = None
     self._mini_view_image1 = None
     self._mini_view_image2 = None
     self._enable_autopilot = args.autopilot
     self._enable_imitation = args.imitation
     self._lidar_measurement = None
     self._map_view = None
     self._is_on_reverse = False
     self._display_map = args.map
     self._city_name = None
     self._map = None
     self._map_shape = None
     self._map_view = None
     self._position = None
     self._agent_positions = None
     self.model = lm('./model_semseg.hdf5')
     self.img_array = None
     self.count = -3
Exemplo n.º 13
0
                    batch_size=512,
                    epochs=50,
                    class_weight='auto',
                    validation_data=(X_val, y_val),
                    callbacks=([
                        EarlyStopping(monitor='val_loss',
                                      mode='min',
                                      verbose=1,
                                      patience=20),
                        ModelCheckpoint("CNN_model.hdf5",
                                        monitor="val_loss",
                                        save_best_only=True)
                    ]))

# plot loss
model = lm('CNN_model.hdf5')
plt.style.use('ggplot')
plt.figure()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
plt.savefig("CNN Model loss.png")

#---------------------------- Prediction and Evaluation -------------------------------
#get the predict values
y_pred = model.predict(X_test)
threshold = 0.5
Exemplo n.º 14
0
 def __init__(self, load_model=False, model_path=None):
     self.model = lm('duckietown_il/keras/trained_models/.h5')
     self.current_image = np.zeros(expect_shape)
Exemplo n.º 15
0
    def __init__(
        self,
        wordvectors_file_vec='wiki.es.vec',
        w2v_limit=None,
        best_epoch=200,
        mimick_weights_file='Results Yoshio/Model/run_1/model-%d.hdf5',
        LSTM_HIDDEN_UNITS=150,
        CHAR_VECTOR_WORD_SIZE=20,
        VECTOR_WORD_SIZE=300,
        DEFAULT_DICTIONARY={
            'a': 1,
            'b': 2,
            'c': 3,
            'd': 4,
            'e': 5,
            'f': 6,
            'g': 7,
            'h': 8,
            'i': 9,
            'j': 10,
            'k': 11,
            'l': 12,
            'm': 13,
            'n': 14,
            'ñ': 15,
            'o': 16,
            'p': 17,
            'q': 18,
            'r': 19,
            's': 20,
            't': 21,
            'u': 22,
            'v': 23,
            'w': 24,
            'x': 25,
            'y': 26,
            'z': 27,
            'á': 28,
            'é': 29,
            'í': 30,
            'ó': 31,
            'ú': 32,
            '1': 33,
            '2': 34,
            '3': 35,
            '4': 36,
            '5': 37,
            '6': 38,
            '7': 39,
            '8': 40,
            '9': 41,
            '0': 42
        }):
        self.wordvectors_file_vec = wordvectors_file_vec
        self.w2v_limit = w2v_limit
        self.best_epoch = best_epoch
        self.mimick_weights_file = mimick_weights_file
        self.LSTM_HIDDEN_UNITS = LSTM_HIDDEN_UNITS
        self.CHAR_VECTOR_WORD_SIZE = CHAR_VECTOR_WORD_SIZE
        self.VECTOR_WORD_SIZE = VECTOR_WORD_SIZE
        self.DEFAULT_DICTIONARY = DEFAULT_DICTIONARY

        #self.graph = tf.get_default_graph() #tf.Graph()
        #self.session = tf.Session()

        self.wordvectors = self.__load_vectors()
        self.vocabulary = list(self.wordvectors.vocab.keys())
        self.vocabulary_size = len(self.vocabulary)
        self.model = self.__create_model()
        self.model = lm(self.mimick_weights_file % self.best_epoch,
                        custom_objects={
                            'euclidean_distance_loss':
                            self.__euclidean_distance_loss
                        })
        self.model._make_predict_function()
Exemplo n.º 16
0
def load_model():
    # load the pre-trained model for example in h5 format from keras
    global model
    model = lm("CNN_Cifar10_Model.h5")
    return model
Exemplo n.º 17
0
def load_model(model_path):
    classifier = lm(model_path)
    classifier.summary()
    sequence_length = classifier.layers[1].input_shape[2]

    return classifier, sequence_length
Exemplo n.º 18
0
 def load_model(self, model_file):
     return lm(model_file)
Exemplo n.º 19
0
def main(argv):
    # builds the model
    curmodel = lm(argv[0])
    datafile = argv[1]
    return update(curmodel, datafile)
Exemplo n.º 20
0
 def load_model(self, model_name):
     self.model = lm(model_name)
Exemplo n.º 21
0
def main(argv):
    curmodel = lm(argv[0])
    datafile = argv[1]
    return update(curmodel, datafile)
Exemplo n.º 22
0
 def load_modal(self):
     if self.soft_max:
         return lm(self.modal_path)
Exemplo n.º 23
0
        skip_learning = True

    # Create Doom instance
    game = initialize_vizdoom(config_file_path)

    # Action = which buttons are pressed
    n = game.get_available_buttons_size()
    actions = [list(a) for a in it.product([0, 1], repeat=n)]
    actions = [[0, 0, 1], [0, 1, 0], [1, 0, 0]]

    # Create replay memory which will store the transitions
    memory = ReplayMemory(capacity=replay_memory_size)

    if load_model:
        print("Loading model from: ", model_savefile)
        current_model = lm(model_savefile)
        pass
    else:
        my_input, current_model = create_model(len(actions))
        _, updated_model = create_model(len(actions))

    print("Starting the training!")
    time_start = time()
    if not skip_learning:
        for epoch in range(epochs):
            print("\nEpoch %d\n-------" % (epoch + 1))
            train_episodes_finished = 0
            train_scores = []
            print("Training...")
            game.new_episode()
            memory.reset_test_buffer()
Exemplo n.º 24
0
def run_carla_client(args):
    model = lm('/home/ritvik/PythonClient/model_RGB.hdf5')   
    # Here we will run 3 episodes with 300 frames each.
    number_of_episodes = 3
    frames_per_episode =900

    # We assume the CARLA server is already waiting for a client to connect at
    # host:port. To create a connection we can use the `make_carla_client`
    # context manager, it creates a CARLA client object and starts the
    # connection. It will throw an exception if something goes wrong. The
    # context manager makes sure the connection is always cleaned up on exit.
    with make_carla_client(args.host, args.port) as client:
        print('CarlaClient connected')

        for episode in range(0, number_of_episodes):
            # Start a new episode.

            if args.settings_filepath is None:

                # Create a CarlaSettings object. This object is a wrapper around
                # the CarlaSettings.ini file. Here we set the configuration we
                # want for the new episode.
                settings = CarlaSettings()
                settings.set(
                    SynchronousMode=True,
                    SendNonPlayerAgentsInfo=True,
                    NumberOfVehicles=20,
                    NumberOfPedestrians=40,
                    WeatherId=random.choice([1, 3, 7, 8, 14]),
                    QualityLevel=args.quality_level)
                settings.randomize_seeds()

                # Now we want to add a couple of cameras to the player vehicle.
                # We will collect the images produced by these cameras every
                # frame.

                # The default camera captures RGB images of the scene.
                camera0 = Camera('SemanticSegmentation',PostProcessing='Semantic Segmentation')
                # Set image resolution in pixels.
                camera0.set_image_size(800, 600)
                # Set its position relative to the car in meters.
                camera0.set_position(0.30, 0, 1.30)
                settings.add_sensor(camera0)

                # Let's add another camera producing ground-truth depth.
                camera1 = Camera('CameraDepth', PostProcessing='Depth')
                camera1.set_image_size(800, 600)
                camera1.set_position(0.30, 0, 1.30)
                settings.add_sensor(camera1)

                if args.lidar:
                    lidar = Lidar('Lidar32')
                    lidar.set_position(0, 0, 2.50)
                    lidar.set_rotation(0, 0, 0)
                    lidar.set(
                        Channels=32,
                        Range=50,
                        PointsPerSecond=100000,
                        RotationFrequency=10,
                        UpperFovLimit=10,
                        LowerFovLimit=-30)
                    settings.add_sensor(lidar)

            else:

                # Alternatively, we can load these settings from a file.
                with open(args.settings_filepath, 'r') as fp:
                    settings = fp.read()

            # Now we load these settings into the server. The server replies
            # with a scene description containing the available start spots for
            # the player. Here we can provide a CarlaSettings object or a
            # CarlaSettings.ini file as string.
            scene = client.load_settings(settings)

            # Choose one player start at random.
            number_of_player_starts = len(scene.player_start_spots)
            player_start = random.randint(0, max(0, number_of_player_starts - 1))

            # Notify the server that we want to start the episode at the
            # player_start index. This function blocks until the server is ready
            # to start the episode.
            print('Starting new episode...')
            client.start_episode(player_start)

            # Iterate every frame in the episode.
            for frame in range(0, frames_per_episode):

                # Read the data produced by the server this frame.
                measurements, sensor_data = client.read_data()

                # Print some of the measurements.
                print_measurements(measurements)

                # Save the images to disk if requested.
                if args.save_images_to_disk:
                    for name, measurement in sensor_data.items():
                        filename = args.out_filename_format.format(episode, name, frame)
                        measurement.save_to_disk(filename)
                
                if args.send_controls_from_nn:
                    for name,measurement in sensor_data.items():
                        img = measurement.return_image()
                        img_array = np.asarray(img)

                        # print("shape of img_array is " + str(np.shape(img_array)))
                                               
                        img_array = cv2.resize(img_array, dsize=(240, 320), interpolation=cv2.INTER_CUBIC)
                        # print("shape of img_array is " + str(np.shape(img_array)))

                        normalized_img = np.zeros((240,320,3))
                        normalized_img = cv2.normalize(img_array,normalized_img,0,255,cv2.NORM_MINMAX)
                        # print("Shape of norm_img is " + str(np.shape(normalized_img)))


                        normalized_img = normalized_img.reshape(1,240,320,3)

                        control = measurements.player_measurements.autopilot_control
                        

                        if abs(float(model.predict(normalized_img,batch_size=1))) < 50.0:
                            control.steer = float(model.predict(normalized_img,batch_size=1))
                        else:   
                            print("Entered")

                        print("Steer value is " + str(control.steer))


                       # # control.steer += random.uniform(-0.1, 0.1)
                       #  img_array = np.asarray(img)
                       #  np.reshape(img_array, img_array.shape + (1,))
                       #  control.steer =float(model.predict(img_array[None,:,:,:], batch_size=1))
                       #  print("Control steer is " + str(control.steer))
                       #  if(abs(control.steer)<0.10):
                       #      control.steer = 0.0
                       #  control.throttle = 0.5
                       #  print("Throttle value is " + str(control.throttle))
                        client.send_control(control)
                # We can access the encoded data of a given image as numpy
                # array using its "data" property. For instance, to get the
                # depth value (normalized) at pixel X, Y
                #
                #     depth_array = sensor_data['CameraDepth'].data
                #     value_at_pixel = depth_array[Y, X]
                #

                # Now we have to send the instructions to control the vehicle.
                # If we are in synchronous mode the server will pause the
                # simulation until we send this control.

                if args.autopilot:
                    control = measurements.player_measurements.autopilot_control
                    #control.steer += random.uniform(-0.1, 0.1)
                    client.send_control(control)
                    print(str(control.steer))
Exemplo n.º 25
0
    print(("Testing" if skip_learning else "Training"), 'KFrames:', kframes,
          'Frame Repeat:', frame_repeat)

    # Create Doom instance
    game = initialize_vizdoom(config_file_path)

    # Action = which buttons are pressed
    n = game.get_available_buttons_size()
    actions = [list(a) for a in it.product([0, 1], repeat=n)]

    # Create replay memory which will store the transitions
    memory = ReplayMemory(capacity=replay_memory_size)

    if load_model and os.path.isfile(model_savefile):
        print("Loading model from: ", model_savefile)
        model = lm(model_savefile)
    else:
        my_input, model = create_model(len(actions))

    print("Starting the training!")
    time_start = time()
    if not skip_learning:
        for epoch in range(epochs):
            print("\nEpoch %d\n-------" % (epoch + 1))
            train_episodes_finished = 0
            train_scores = []
            memory.reset_test_buffer()

            print("Training...")
            game.new_episode()
            for learning_step in trange(learning_steps_per_epoch, leave=True):
Exemplo n.º 26
0
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

print('first run 5.2.py')

from keras import models
from keras.models import load_model as lm
from keras.preprocessing import image

m1 = lm('cats_and_dogs_small_1.h5')
img_path = '/home/wb/Pictures/abc.png'
#'/home/wb/temp/cats_and_dogs_small/train/cats/cat.45.jpg'
img = image.load_img(img_path, target_size=(150, 150))
img_tensor0 = image.img_to_array(img)
img_tensor1 = np.expand_dims(img_tensor0, axis=0)
img_tensor2 = img_tensor1 / 255
print(img_tensor2.shape)
plt.imshow(img_tensor2[0])

layer_inputs = m1.input
layer_outputs = [layer.output for layer in m1.layers[:8]]
activation_model = models.Model(inputs=layer_inputs, outputs=layer_outputs)
activations = activation_model.predict(img_tensor2)


def call_this():
    print('which layer you wanna view?')