示例#1
0
    def image_regression(self,
                         output_dim: int = None,
                         **kwargs) -> ak.ImageRegressor:
        """Image Regression.

        Args:
            output_dim (int, optional): Number of output dimensions. Defaults to None.

        Returns:
            ak.ImageRegressor: AutoKERAS image regression class.
        """
        return ak.ImageRegressor(
            output_dim=output_dim,
            loss=self.loss,
            metrics=self.metrics,
            project_name=self.project_name,
            max_trials=self.max_trials,
            directory=self.directory,
            objective=self.objective,
            tuner=self.tuner,
            overwrite=self.overwrite,
            seed=self.seed,
            max_model_size=self.max_model_size,
            **kwargs,
        )
示例#2
0
def test_img_reg_fit_call_auto_model_fit(fit, tmp_path):
    auto_model = ak.ImageRegressor(directory=tmp_path, seed=utils.SEED)

    auto_model.fit(x=utils.generate_data(num_instances=100, shape=(32, 32, 3)),
                   y=utils.generate_data(num_instances=100, shape=(1, )))

    assert fit.is_called
示例#3
0
def test_image_regressor(tmp_path):
    train_x = utils.generate_data(num_instances=320, shape=(32, 32, 3))
    train_y = utils.generate_data(num_instances=320, shape=(1,))
    clf = ak.ImageRegressor(directory=tmp_path, max_trials=2, seed=utils.SEED)
    clf.fit(train_x, train_y, epochs=1, validation_split=0.2)
    clf.export_model()
    assert clf.predict(train_x).shape == (len(train_x), 1)
示例#4
0
def train_autokeras(l=None):
    if l is None:
        l = get_data()
    dirname = os.path.join(_mydir, 'autokeras')
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    model = ak.ImageRegressor(path=dirname)
    # TODO fix this shape ...
    model.fit(np.atleast_3d(l.X_train.values), l.y_train.values.squeeze())
    return attributedict_from_locals('model')
示例#5
0
 def build_model(self) -> ak.AutoModel:
     model = None
     if self.data_type == 'image':
         if self.task_type == 'regression':
             model = ak.ImageRegressor()
         elif self.task_type == 'classification':
             model = ak.ImageClassifier()
     elif self.data_type == 'text':
         if self.task_type == 'regression':
             model = ak.TextRegressor()
         elif self.task_type == 'classification':
             model = ak.TextRegressor()
     elif self.data_type == 'csv':
         if self.task_type == 'regression':
             model = ak.StructuredDataRegressor()
         elif self.task_type == 'classification':
             model = ak.StructuredDataClassifier()
     return model
示例#6
0
文件: train.py 项目: ccaleanu/Gaze
def train_ak_regression():
    print("Train MPII: AutoKeras Regression")
    x_train, y_train, x_test, y_test = loaddb.load_mpii(config.dbpath_mpii)

    reg = ak.ImageRegressor(overwrite=True,
                            max_trials=config.max_trials,
                            directory=config.outpath_mpii)

    reg.fit(x_train,
            y_train,
            validation_data=(x_test, y_test),
            epochs=config.epochs)

    print(reg.evaluate(x_test, y_test))

    model = reg.export_model()

    print(type(model))
    # model.save(config.out_path + "model_ak_imgRegr.h5")

    return 0
示例#7
0
def test_image_regressor(_, tmp_dir):
    x_train = np.random.rand(100, 32, 32, 3)
    y_train = np.random.rand(100, 1)
    clf = ak.ImageRegressor(directory=tmp_dir, max_trials=2)
    clf.fit(x_train, y_train, epochs=2, validation_split=0.2)
import autokeras as ak

# Load x, y dataset
x, y = Utils.load_dataset('Data', task='regression')

# Split x, y dataset
# train-test split
x_train, x_test, y_train, y_test = train_test_split(x,
                                                    y,
                                                    train_size=0.8,
                                                    random_state=123)
# train-val split
x_train, x_val, y_train, y_val = train_test_split(x_train,
                                                  y_train,
                                                  train_size=0.8,
                                                  random_state=456)

# ImageRegressor model
model = ak.ImageRegressor(metrics=['mae', 'mape'],
                          max_trials=100,
                          overwrite=True,
                          seed=45)

# Train model
model.fit(x_train, y_train, epochs=200, validation_data=(x_val, y_val))

# Evaluate model
score = model.evaluate(x_test, y_test)

# Inference time - use magic command %timeit in IPython or IDE
# %timeit model.predict_on_batch(x_test)
示例#9
0
def test_image_regressor(tmp_dir):
    x_train = np.random.rand(100, 32, 32, 3)
    y_train = np.random.rand(100)
    clf = ak.ImageRegressor(directory=tmp_dir)
    clf.fit(x_train, y_train, epochs=2, trials=2)
    assert clf.predict(x_train).shape == (100,)
    'data': "200k",
}

# ================== Import Data ==================
DATA_PATH = get_git_root() + "data/simulated/"
images = np.load(DATA_PATH + f"images_{config['data']}.npy")
images = images.reshape(images.shape[0], 16, 16, 1)
positions = np.load(DATA_PATH + "positions_200k.npy")
labels = np.load(DATA_PATH + "labels_200k.npy")

single_indices, double_indices, close_indices = event_indices(positions)
train_idx, val_idx, non1, non2 = train_test_split(
    double_indices, double_indices, random_state=config['random_seed'])
# log-scale the images if desireable
config['scaling'] = "minmax"
# set tf random seed
with tf.device(get_tf_device(20)):
	reg = ak.ImageRegressor(
	    overwrite=True,
	    max_trials=100,
	)
	# Feed the structured data regressor with training data.
	reg.fit(
	    normalize_image_data(images[train_idx]),
	    normalize_position_data(positions[train_idx]),
	    validation_data=(normalize_image_data(images[val_idx]), normalize_position_data(positions[val_idx])),
	    epochs=10,
	)
	predicted_y = reg.predict(normalize_image_data(images[val_idx]))
	print(reg.evaluate(normalize_image_data(images[val_idx]), normalize_position_data(positions[val_idx])))
示例#11
0
def train_keras(model_name,
                folder,
                nr_images_batch,
                nr_batch,
                nr_epoch,
                max_trials=10):
    """Training function for the regression

    Args:
        model_name (str): the name of the segmentation model
        folder (str): the path to the images
        nr_images_batch (int): number of images per batch
        nr_batch (int): number of batches
        nr_epoch (int): number of epochs
        max_trials (int, optional): number of trails for AutoKeras. Defaults to 10.

    Returns:
        mse: metric Mean Squared Error
        rmse: metric Root Mean Squared Error
        mae: metric Mean Absolut Error
        model: the trained model
    """

    image_width = 192
    image_height = 192
    ratio = 1
    channels = 3

    logdir = "logs/scalars/" + datetime.now().strftime("%Y%m%d-%H%M%S")
    tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)

    allfiles = [
        f for f in os.listdir(folder)
        if os.path.isfile(os.path.join(folder, f))
    ]

    for i in range(0, nr_batch):
        print("--- Batch {} --- ".format(str(i)))
        # Initialize the image regressor.
        reg = ak.ImageRegressor(
            project_name=model_name,
            #  metrics=[tf.keras.metrics.MeanSquaredError(),
            #          tf.keras.metrics.RootMeanSquaredError(),
            #          tf.keras.metrics.MeanAbsoluteError()],
            overwrite=False,
            max_trials=max_trials)

        onlyfiles = allfiles[i * nr_images_batch:(i + 1) * nr_images_batch]
        print("select from to", i * nr_images_batch, (i + 1) * nr_images_batch)

        train_files = []
        y_train = np.ndarray(len(onlyfiles))

        for _file in onlyfiles:
            train_files.append(_file)
            y_train[i] = (np.float64(_file.split('_')[11]))
        print("Files in train_files: %d" % len(train_files))

        image_width = int(image_width / ratio)
        image_height = int(image_height / ratio)

        dataset = np.ndarray(shape=(len(train_files), image_height,
                                    image_width, channels),
                             dtype=np.float32)

        j = 0
        for _file in train_files:
            img = load_img(folder + "/" + _file)  # this is a PIL image
            #img.thumbnail((image_width, image_height))
            # Convert to Numpy Array
            x = img_to_array(img)
            #x = x.reshape((3, 48, 48))
            # Normalize
            #x = (x - 128.0) / 128.0
            dataset[i] = x
            j += 1
            if j % 1000 == 0:
                print("%d images to array" % j)
        print("All images to array!")

        #Splitting
        X_train = dataset
        #X_train, X_test, y_train, y_test = train_test_split(dataset, y_train, test_size=0.2, random_state=33)
        #X_test, X_val, y_test, y_val = train_test_split(X_test, y_test, test_size=0.5, random_state=33)
        #print("Train set size: {0}, Val set size: {1}, Test set size: {2}".format(len(X_train), len(X_val), len(X_test)))
        #print(y_train[0])

        # Feed the image regressor with training data.
        if i + 1 == nr_batch:
            print("Last run, eval the model")
            y_hat = reg.predict(dataset)
            m = tf.keras.metrics.MeanSquaredError(name="mean_squared_error",
                                                  dtype=None)
            m.update_state(y_hat, y_train)
            mse = m.result().numpy()
            m = tf.keras.metrics.RootMeanSquaredError(
                name="root_mean_squared_error", dtype=None)
            m.update_state(y_hat, y_train)
            rmse = m.result().numpy()
            m = tf.keras.metrics.MeanAbsoluteError(name="mean_absolute_error",
                                                   dtype=None)
            m.update_state(y_hat, y_train)
            mae = m.result().numpy()
            return reg, mse, rmse, mae
        else:
            reg.fit(X_train,
                    y_train,
                    validation_split=0.2,
                    batch_size=25,
                    epochs=nr_epoch)
train_dir='/data/training.csv'
test_dir='/data/test.csv'
train=pd.read_csv(train_dir)
test=pd.read_csv(test_dir)

train=train.dropna()
train=train.reset_index(drop=True)

X_train=[]
Y_train=[]

for img in train['Image']:
    X_train.append(np.asarray(img.split(),dtype=float).reshape(96,96,1))
X_train=np.reshape(X_train,(-1,96,96,1))
X_train = np.asarray(X_train).astype('float32')
    
for i in range(len((train))): 
    Y_train.append(np.asarray(train.iloc[i][0:30].to_numpy()))
Y_train = np.asarray(Y_train).astype('float32')


## Data training

reg = ak.ImageRegressor(max_trials=MAX_TRIALS)
reg.fit(X_train, Y_train, validation_split=0.15, epochs=EPOCHS)

# Export trained model to externally attached pvc 
my_model = reg.export_model()
my_model.save('/data/model_autokeras', save_format="tf")
示例#13
0
if __name__ == '__main__':
    ad_size = 1
    # Load and select data
    x_train, x_test, y_train, y_test = LoadAndSelectData(ad_size)

    x_train = x_train.reshape(int(x_train.size / ad_size/7), ad_size, 7, 1)
    x_test = x_test.reshape(int(x_test.size / ad_size/7), ad_size, 7, 1)
    print((x_train.shape, y_train.shape))
    print((x_test.shape, y_test.shape))

    x_train = x_train.astype('int8')
    y_train = y_train.astype('int8')
    x_test = x_train.astype('int8')
    y_test = y_train.astype('int8')
    #print('Training data shape:%d' % (min(x_train.flatten())))
    x_train = (x_train / 128).astype('float32')
    x_test = (x_test / 128).astype('float32')
    y_train = ((y_train)/128).astype('float32')
    y_test = ((y_test) / 128).astype('float32')

    # Initialize the classifier.
    reg = ak.ImageRegressor(max_trials=20)
    # x is the path to the csv file. y is the column name of the column to predict.
    reg.fit(x=x_train, y=y_train, epochs=30)
    # Evaluate the accuracy of the found model.
    print(reg.evaluate(x=x_test, y=y_test))

    model = reg.export_model()

    model.save("autokeras")
示例#14
0
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, Dropout
import autokeras as ak
from tensorflow.keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
print(y_train[:3])

model = ak.ImageRegressor(
    overwrite=True,
    max_trials=3,
    loss='mae',
    metrics=['mse'],
)

#model.summary() #안먹힘

from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras.callbacks import ReduceLROnPlateau
es = EarlyStopping(monitor='val_loss', mode='min', patience=6)
lr = ReduceLROnPlateau(monitor='val_loss',
                       mode='auto',
                       patience=3,
                       factor=0.2,
                       verbose=2)
ck = ModelCheckpoint('C:/data/MC/',
                     save_weight_only=True,
                     save_best_only=True,
                     monitor='val_loss',
示例#15
0
def test_image_regressor(tmp_dir):
    train_x = common.generate_data(num_instances=100, shape=(32, 32, 3))
    train_y = common.generate_data(num_instances=100, shape=(1, ))
    clf = ak.ImageRegressor(directory=tmp_dir, max_trials=2, seed=common.SEED)
    clf.fit(train_x, train_y, epochs=1, validation_split=0.2)
    assert clf.predict(train_x).shape == (len(train_x), 1)
示例#16
0
    ages = train_set["age"].astype("int").to_numpy()
    return image_inputs, ages


train_set = df[df["full_path"] < "02"].sample(200)
train_imgs, train_ages = df2numpy(train_set)

test_set = df[df["full_path"] < "02"].sample(100)
test_imgs, test_ages = df2numpy(test_set)
"""
### **Training using AutoKeras**
"""

# Initialize the image regressor
reg = ak.ImageRegressor(max_trials=15)  # AutoKeras tries 15 different models.

# Find the best model for the given training data
reg.fit(train_imgs, train_ages)

# Predict with the chosen model:
# predict_y = reg.predict(test_images)  # Uncomment if required

# Evaluate the chosen model with testing data
print(reg.evaluate(test_images, test_ages))
"""
### **Validation Data**

By default, AutoKeras use the last 20% of training data as validation data. As shown in
the example below, you can use validation_split to specify the percentage.
"""
model.save(fr'mymodel1.h5')

!pip install folium==0.2.1
!pip install imgaug==0.2.7
#!pip install autokeras

!pip uninstall pytorch torchvision -y
!pip3 install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp36-cp36m-win_amd64.whl
!pip3 install torchvision



import autokeras as ak

model_ak = ak.ImageRegressor(verbose = True)
model_ak.fit(Xgen,Ygen, time_limit=60*60)

score = model_ak.evaluate(X_val, y_val)
print(score)

ypred = model_ak.predict(X)

plt.plot(y,'g', ypred,'r')
plt.show()

model_ak.export_autokeras_model("autoKeras.pkl")

from autokeras.utils import pickle_from_file

model = pickle_from_file('autoKeras.pkl')