윤영빈

changed saving method

......@@ -92,7 +92,6 @@ class VideoFileUploadView(APIView):
return Response(result, status=status.HTTP_201_CREATED)
else:
return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class VideoFileList(APIView):
......
{"model": "NetVLADModelLF", "feature_sizes": "1024,128", "feature_names": "rgb,audio", "frame_features": true, "label_loss": "CrossEntropyLoss"}
\ No newline at end of file
......@@ -33,17 +33,17 @@ FLAGS = flags.FLAGS
if __name__ == "__main__":
# Dataset flags.
flags.DEFINE_string(
"train_dir", "F:/yt8mDataset/savedModel",
"train_dir", "F:/yt8mDataset/savedModelaa2",
"The directory to load the model files from. "
"The tensorboard metrics files are also saved to this "
"directory.")
flags.DEFINE_string(
"eval_data_pattern", "",
"eval_data_pattern", "F:/yt8mDataset/train2/train*.tfrecord",
"File glob defining the evaluation dataset in tensorflow.SequenceExample "
"format. The SequenceExamples are expected to have an 'rgb' byte array "
"sequence feature as well as a 'labels' int64 context feature.")
flags.DEFINE_bool(
"segment_labels", False,
"segment_labels", True,
"If set, then --eval_data_pattern must be frame-level features (but with"
" segment_labels). Otherwise, --eval_data_pattern must be aggregated "
"video-level features. The model must also be set appropriately (i.e. to "
......@@ -54,7 +54,7 @@ if __name__ == "__main__":
"How many examples to process per batch.")
flags.DEFINE_integer("num_readers", 8,
"How many threads to use for reading input files.")
flags.DEFINE_boolean("run_once", False, "Whether to run eval only once.")
flags.DEFINE_boolean("run_once", True, "Whether to run eval only once.")
flags.DEFINE_integer("top_k", 5, "How many predictions to output per video.")
......
......@@ -46,7 +46,7 @@ flags.DEFINE_string(
"Some Frame-Level models can be decomposed into a "
"generalized pooling operation followed by a "
"classifier layer")
flags.DEFINE_integer("lstm_cells", 512, "Number of LSTM cells.")
flags.DEFINE_integer("lstm_cells", 1024, "Number of LSTM cells.")
flags.DEFINE_integer("lstm_layers", 2, "Number of LSTM layers.")
......
......@@ -23,7 +23,7 @@ import tarfile
import tempfile
import time
import numpy as np
import ssl
import readers
from six.moves import urllib
import tensorflow as tf
......@@ -39,11 +39,11 @@ FLAGS = flags.FLAGS
if __name__ == "__main__":
# Input
flags.DEFINE_string(
"train_dir", "", "The directory to load the model files from. We assume "
"train_dir", "/mnt/f/yt8mDataset/savedModelaa", "The directory to load the model files from. We assume "
"that you have already run eval.py onto this, such that "
"inference_model.* files already exist.")
flags.DEFINE_string(
"input_data_pattern", "",
"input_data_pattern", "/mnt/f/yt8mDataset/train2/train*.tfrecord",
"File glob defining the evaluation dataset in tensorflow.SequenceExample "
"format. The SequenceExamples are expected to have an 'rgb' byte array "
"sequence feature as well as a 'labels' int64 context feature.")
......@@ -60,7 +60,7 @@ if __name__ == "__main__":
"be created and the contents of the .tgz file will be "
"untarred here.")
flags.DEFINE_bool(
"segment_labels", False,
"segment_labels", True,
"If set, then --input_data_pattern must be frame-level features (but with"
" segment_labels). Otherwise, --input_data_pattern must be aggregated "
"video-level features. The model must also be set appropriately (i.e. to "
......@@ -69,18 +69,18 @@ if __name__ == "__main__":
"Limit total number of segment outputs per entity.")
flags.DEFINE_string(
"segment_label_ids_file",
"https://raw.githubusercontent.com/google/youtube-8m/master/segment_label_ids.csv",
"/mnt/e/khuhub/2015104192/web/backend/yt8m/vocabulary.csv",
"The file that contains the segment label ids.")
# Output
flags.DEFINE_string("output_file", "", "The file to save the predictions to.")
flags.DEFINE_string("output_file", "/mnt/f/yt8mDataset/kaggle_solution_validation_temp.csv", "The file to save the predictions to.")
flags.DEFINE_string(
"output_model_tgz", "",
"If given, should be a filename with a .tgz extension, "
"the model graph and checkpoint will be bundled in this "
"gzip tar. This file can be uploaded to Kaggle for the "
"top 10 participants.")
flags.DEFINE_integer("top_k", 20, "How many predictions to output per video.")
flags.DEFINE_integer("top_k", 5, "How many predictions to output per video.")
# Other flags.
flags.DEFINE_integer("batch_size", 512,
......@@ -108,18 +108,15 @@ def format_lines(video_ids, predictions, top_k, whitelisted_cls_mask=None):
def get_input_data_tensors(reader, data_pattern, batch_size, num_readers=1):
"""Creates the section of the graph which reads the input data.
Args:
reader: A class which parses the input data.
data_pattern: A 'glob' style path to the data files.
batch_size: How many examples to process at a time.
num_readers: How many I/O threads to use.
Returns:
A tuple containing the features tensor, labels tensor, and optionally a
tensor containing the number of frames per video. The exact dimensions
depend on the reader being used.
Raises:
IOError: If no files matching the given pattern were found.
"""
......@@ -243,12 +240,18 @@ def inference(reader, train_dir, data_pattern, out_file_location, batch_size,
whitelisted_cls_mask = np.zeros((predictions_tensor.get_shape()[-1],),
dtype=np.float32)
segment_label_ids_file = FLAGS.segment_label_ids_file
"""
if segment_label_ids_file.startswith("http"):
logging.info("Retrieving segment ID whitelist files from %s...",
segment_label_ids_file)
segment_label_ids_file, _ = urllib.request.urlretrieve(segment_label_ids_file)
"""
if segment_label_ids_file.startswith("http"):
logging.info("Retrieving segment ID whitelist files from %s...",
segment_label_ids_file)
segment_label_ids_file, _ = urllib.request.urlretrieve(
segment_label_ids_file)
with tf.io.gfile.GFile(segment_label_ids_file) as fobj:
with tf.io.gfile.GFile(segment_label_ids_file) as fobj:
for line in fobj:
try:
cls_id = int(line)
......@@ -307,6 +310,7 @@ def inference(reader, train_dir, data_pattern, out_file_location, batch_size,
heaps = {}
out_file.seek(0, 0)
for line in out_file:
print(line)
segment_id, preds = line.decode("utf8").split(",")
if segment_id == "VideoId":
# Skip the headline.
......@@ -382,4 +386,4 @@ def main(unused_argv):
if __name__ == "__main__":
app.run()
app.run()
\ No newline at end of file
......
......@@ -41,11 +41,11 @@ FLAGS = flags.FLAGS
if __name__ == "__main__":
# Input
flags.DEFINE_string(
"train_dir", "", "The directory to load the model files from. We assume "
"train_dir", "E:/savedModel", "The directory to load the model files from. We assume "
"that you have already run eval.py onto this, such that "
"inference_model.* files already exist.")
flags.DEFINE_string(
"input_data_pattern", "",
"input_data_pattern", "F:/yt8mDataset/test/train*.tfrecord",
"File glob defining the evaluation dataset in tensorflow.SequenceExample "
"format. The SequenceExamples are expected to have an 'rgb' byte array "
"sequence feature as well as a 'labels' int64 context feature.")
......@@ -62,7 +62,7 @@ if __name__ == "__main__":
"be created and the contents of the .tgz file will be "
"untarred here.")
flags.DEFINE_bool(
"segment_labels", False,
"segment_labels", True,
"If set, then --input_data_pattern must be frame-level features (but with"
" segment_labels). Otherwise, --input_data_pattern must be aggregated "
"video-level features. The model must also be set appropriately (i.e. to "
......@@ -75,7 +75,7 @@ if __name__ == "__main__":
"The file that contains the segment label ids.")
# Output
flags.DEFINE_string("output_file", "", "The file to save the predictions to.")
flags.DEFINE_string("output_file", "F:/yt8mDataset/kaggle_solution_validation_temp.csv", "The file to save the predictions to.")
flags.DEFINE_string(
"output_model_tgz", "",
"If given, should be a filename with a .tgz extension, "
......@@ -85,7 +85,7 @@ if __name__ == "__main__":
flags.DEFINE_integer("top_k", 5, "How many predictions to output per video.")
# Other flags.
flags.DEFINE_integer("batch_size", 32,
flags.DEFINE_integer("batch_size", 16,
"How many examples to process per batch.")
flags.DEFINE_integer("num_readers", 1,
"How many threads to use for reading input files.")
......@@ -269,6 +269,7 @@ def inference(reader, train_dir, data_pattern, out_file_location, batch_size,
except ValueError:
# Simply skip the non-integer line.
continue
fobj.close()
out_file.write(u"VideoId,LabelConfidencePairs\n".encode("utf8"))
......@@ -286,8 +287,10 @@ def inference(reader, train_dir, data_pattern, out_file_location, batch_size,
vocabs.close()
try:
while not coord.should_stop():
print("CAME IN")
video_id_batch_val, video_batch_val, num_frames_batch_val = sess.run(
[video_id_batch, video_batch, num_frames_batch])
print("SESS OUT")
if FLAGS.segment_labels:
results = get_segments(video_batch_val, num_frames_batch_val, 5)
video_segment_ids = results["video_segment_ids"]
......@@ -333,6 +336,7 @@ def inference(reader, train_dir, data_pattern, out_file_location, batch_size,
segment_classes = []
cls_result_arr = []
cls_score_dict = {}
resultList = []
out_file.seek(0, 0)
old_seg_name = '0000'
counter = 0
......@@ -344,12 +348,30 @@ def inference(reader, train_dir, data_pattern, out_file_location, batch_size,
if segment_id == "VideoId":
# Skip the headline.
continue
print(line)
preds = preds.split(" ")
"""
pred_cls_ids = [int(preds[idx]) for idx in range(0, len(preds), 2)]
pred_cls_scores = [float(preds[idx]) for idx in range(1, len(preds), 2)]
#=======================================
segment_id = str(segment_id.split(":")[0])
"""
"""
denom = 1.0
for i in range(0,top_k):
denom = denom + float(preds[(i*2) + 1])
print("DENOM = ",denom)
for i in range(0,top_k):
preds[(i*2) + 1] = float(preds[(i*2) + 1])/denom
"""
segment_id = "{0}_{1}".format(str(segment_id.split(":")[0]),str(int(segment_id.split(":")[1])/5))
resultList.append("{0},{1},{2},{3},{4},{5}".format(segment_id,
preds[0]+","+str(preds[1]),
preds[2]+","+str(preds[3]),
preds[4]+","+str(preds[5]),
preds[6]+","+str(preds[7]),
preds[8]+","+str(preds[9])))
#=======================================
"""
if segment_id not in segment_id_list:
segment_id_list.append(str(segment_id))
segment_classes.append("")
......@@ -372,12 +394,12 @@ def inference(reader, train_dir, data_pattern, out_file_location, batch_size,
cls_arr = item.split(" ")[:-1]
cls_arr = list(map(int,cls_arr))
cls_arr = sorted(cls_arr) #클래스별로 정렬
cls_arr = sorted(cls_arr) #sort by class
result_string = ""
temp = cls_score_dict[segs]
temp= sorted(temp.items(), key=operator.itemgetter(1), reverse=True) #밸류값 기준으로 정렬
temp= sorted(temp.items(), key=operator.itemgetter(1), reverse=True) #sort by value
demoninator = float(temp[0][1] + temp[1][1] + temp[2][1] + temp[3][1] + temp[4][1])
#for item in temp:
for itemIndex in range(0, top_k):
......@@ -387,11 +409,16 @@ def inference(reader, train_dir, data_pattern, out_file_location, batch_size,
result_string = result_string + normalized_tag + ":" + format(temp[itemIndex][1]/demoninator,".3f") + ","
cls_result_arr.append(result_string[:-1])
logging.info(segs + " : " + result_string[:-1])
#logging.info(segs + " : " + result_string[:-1])
#=======================================
final_out_file.write("vid_id,segment1,segment2,segment3,segment4,segment5\n")
for seg_id, class_indcies in zip(segment_id_list, cls_result_arr):
final_out_file.write("%s,%s\n" %(seg_id, str(class_indcies)))
"""
final_out_file.write("vid_id,segment1,pred1,segment2,pred2,segment3,pred3,segment4,pred4,segment5,pred5\n")
for resultInfo in resultList:
final_out_file.write(resultInfo)
final_out_file.close()
out_file.close()
......@@ -410,7 +437,7 @@ def main(unused_argv):
os.makedirs(FLAGS.untar_model_dir)
tarfile.open(FLAGS.input_model_tgz).extractall(FLAGS.untar_model_dir)
FLAGS.train_dir = FLAGS.untar_model_dir
print("TRAIN DIR ", FLAGS.input_data_pattern)
flags_dict_file = os.path.join(FLAGS.train_dir, "model_flags.json")
if not file_io.file_exists(flags_dict_file):
raise IOError("Cannot find %s. Did you run eval.py?" % flags_dict_file)
......
......@@ -75,7 +75,7 @@ if __name__ == "__main__":
flags.DEFINE_integer(
"num_gpu", 1, "The maximum number of GPU devices to use for training. "
"Flag only applies if GPUs are installed")
flags.DEFINE_integer("batch_size", 64,
flags.DEFINE_integer("batch_size", 16,
"How many examples to process per batch for training.")
flags.DEFINE_string("label_loss", "CrossEntropyLoss",
"Which loss function to use for training the model.")
......@@ -94,13 +94,13 @@ if __name__ == "__main__":
"Multiply current learning rate by learning_rate_decay "
"every learning_rate_decay_examples.")
flags.DEFINE_integer(
"num_epochs", 5, "How many passes to make over the dataset before "
"num_epochs", 100, "How many passes to make over the dataset before "
"halting training.")
flags.DEFINE_integer(
"max_steps", None,
"max_steps", 100,
"The maximum number of iterations of the training loop.")
flags.DEFINE_integer(
"export_model_steps", 5,
"export_model_steps", 10,
"The period, in number of steps, with which the model "
"is exported for batch prediction.")
......
......@@ -154,6 +154,7 @@ def GetListOfFeatureNamesAndSizes(feature_names, feature_sizes):
List of the feature names and list of the dimensionality of each feature.
Elements in the first/second list are strings/integers.
"""
feature_sizes = str(feature_sizes)
list_of_feature_names = [
feature_names.strip() for feature_names in feature_names.split(",")
]
......