2015-09-24 07:51:47 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright 2015 Carnegie Mellon University
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
import matplotlib as mpl
|
|
|
|
mpl.use('Agg')
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
plt.style.use('bmh')
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
scriptDir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
plotDir = os.path.join(scriptDir, 'plots')
|
|
|
|
workDir = os.path.join(scriptDir, 'work')
|
|
|
|
|
2015-10-12 23:30:29 +08:00
|
|
|
|
2015-09-24 07:51:47 +08:00
|
|
|
def plot(workDirs):
|
|
|
|
trainDfs = []
|
|
|
|
testDfs = []
|
|
|
|
for d in workDirs:
|
|
|
|
trainF = os.path.join(workDir, str(d), 'train.log')
|
|
|
|
testF = os.path.join(workDir, str(d), 'test.log')
|
|
|
|
trainDfs.append(pd.read_csv(trainF, sep='\t'))
|
|
|
|
testDfs.append(pd.read_csv(testF, sep='\t'))
|
|
|
|
if len(trainDfs[-1]) != len(testDfs[-1]):
|
|
|
|
print("Error: Train/test dataframe shapes "
|
|
|
|
"for '{}' don't match: {}, {}".format(
|
|
|
|
d, trainDfs[-1].shape, testDfs[-1].shape))
|
|
|
|
sys.exit(-1)
|
|
|
|
trainDf = pd.concat(trainDfs, ignore_index=True)
|
|
|
|
testDf = pd.concat(testDfs, ignore_index=True)
|
|
|
|
|
2015-10-09 21:54:34 +08:00
|
|
|
print("train, test:")
|
2015-10-12 23:30:29 +08:00
|
|
|
print("\n".join(["{:0.2e}, {:0.2e}".format(x, y) for (x, y) in
|
2015-10-09 21:54:34 +08:00
|
|
|
zip(trainDf['avg triplet loss (train set)'].values[-5:],
|
|
|
|
testDf['avg triplet loss (test set)'].values[-5:])]))
|
2015-09-24 07:51:47 +08:00
|
|
|
|
2015-10-12 23:30:29 +08:00
|
|
|
fig, ax = plt.subplots(1, 1)
|
2015-09-24 07:51:47 +08:00
|
|
|
trainDf.index += 1
|
|
|
|
testDf.index += 1
|
|
|
|
trainDf['avg triplet loss (train set)'].plot(legend='True', ax=ax)
|
2015-10-04 10:47:27 +08:00
|
|
|
testDf['avg triplet loss (test set)'].plot(legend='True', ax=ax, alpha=0.6)
|
2015-10-04 05:24:17 +08:00
|
|
|
plt.legend(['Train loss, semi-hard triplets',
|
|
|
|
'Test loss, random triplets'])
|
2015-09-24 07:51:47 +08:00
|
|
|
plt.xlabel("Epoch")
|
|
|
|
plt.ylabel("Loss")
|
2015-10-04 05:24:17 +08:00
|
|
|
# plt.ylim(ymin=0)
|
2015-09-24 07:51:47 +08:00
|
|
|
plt.xlim(xmin=1)
|
|
|
|
plt.grid(b=True, which='major', color='k', linestyle='-')
|
|
|
|
plt.grid(b=True, which='minor', color='k', linestyle='--', alpha=0.2)
|
2015-10-04 05:24:17 +08:00
|
|
|
ax.set_yscale('log')
|
2015-09-24 07:51:47 +08:00
|
|
|
fig.savefig(os.path.join(plotDir, "loss.pdf"))
|
|
|
|
|
2015-10-12 23:30:29 +08:00
|
|
|
if __name__ == '__main__':
|
2015-09-24 07:51:47 +08:00
|
|
|
os.makedirs(plotDir, exist_ok=True)
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('workDirs', type=int, nargs='+')
|
|
|
|
args = parser.parse_args()
|
|
|
|
plot(args.workDirs)
|