def main(): ValidSampler = Sampler(utils.valid_file) TestSampler = Sampler(utils.test_file) networks = [] weights = [] for i in xrange(5): if i == 0: TrainSampler = Sampler(utils.train_file) prev_ys = np.copy(TrainSampler.labels) else: TrainSampler = Sampler(utils.train_file, prev_ys) network = Network() network.train(TrainSampler) cur_ys = network.predict(TrainSampler) b1 = np.sum(np.multiply(cur_ys, prev_ys)) b2 = np.sum(np.multiply(cur_ys, cur_ys)) w = float(b1) / b2 prev_ys = np.subtract(prev_ys, w * cur_ys) print i, 'done with weight', w network.save('network_' + str(i) + '.ckpt') weights.append(w) networks.append(network) validate_boost(ValidSampler, networks, weights) validate_boost(TestSampler, networks, weights) np.save('weights.npy', weights)
import os import numpy as np from sklearn.tree import DecisionTreeClassifier as Model from cnn import Network DATA_DIR = 'data' pipeline = [ (preprocessing.extract_rgb, True), # (preprocessing.enrich_mirror, False), ] data_file = os.path.join(DATA_DIR, 'data_train.dat') targets_file = os.path.join(DATA_DIR, 'targets_train.dat') data, targets = preprocessing.load_data(data_file), preprocessing.load_data(targets_file) data_train, data_validation, targets_train, targets_validation = sklearn.model_selection.train_test_split( data, targets, test_size=0.25, random_state=42, shuffle=True, stratify=targets ) for func, apply_test in pipeline: data_train, targets_train = func(data_train, targets_train) if apply_test: data_validation, targets_validation = func(data_validation, targets_validation) print(data_train.shape[1:]) model = Network(input_shape=data_train.shape[1:]) model.fit(data_train, targets_train, data_validation, targets_validation) predict = model.predict(data_validation) print(sklearn.metrics.accuracy_score(targets_validation, predict))
args = arg_parse() #Set up the neural network print("Preparing network .....") network = Network(args.cfgfile) network.compile() print("Loading input .....") dataset = Dataset() x_train, y_train, x_test, y_test = dataset.loadData( network.net_info.input_shape) # # Encode the data y_train = to_categorical(y_train) y_test = to_categorical(y_test) print("Training network .....") network.fit(x_train, y_train, x_test, y_test) print("evaluation: ") network.evaluate(x_test, y_test) x_predict, y_predict = dataset.predictData(network.net_info.input_shape) predict_images, predict_labels = dataset.predictImages() print("predicting on remaining images ...") prediction = network.predict(x_predict) network.plotPrediction(predict_images, predict_labels, prediction)