Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions flutter/assets/tasks.pbtxt
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,12 @@ task {
type: COCOGEN
full {
name: "COCO validation set for Stable Diffusion"
input_path: "https://github.com/anhappdev/tmp/releases/download/5/coco_gen_test.tfrecord"
input_path: "https://github.com/anhappdev/tmp/releases/download/6/coco_gen_test.tfrecord"
groundtruth_path: "local:///mlperf_models/stable-diffusion/clip_model_512x512.tflite"
}
lite {
name: "COCO validation set for Stable Diffusion"
input_path: "https://github.com/anhappdev/tmp/releases/download/5/coco_gen_full.tfrecord"
input_path: "https://github.com/anhappdev/tmp/releases/download/6/coco_gen_full.tfrecord"
groundtruth_path: ""
}
tiny {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably we should update the tfrecord file for tiny as well, as the below one will throw an error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. The tiny is for integration test. I will update it later,

Expand Down
21 changes: 12 additions & 9 deletions flutter/cpp/datasets/coco_gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,20 @@ std::vector<uint8_t> CocoGen::ProcessOutput(const int sample_idx,
}
}

std::string raw_output_filename =
raw_output_dir_ + "/output_" + std::to_string(sample_idx) + ".rgb8";
dump_output_pixels(output_pixels, raw_output_filename);

if (!output_pixels.empty()) {
sample_ids_.insert(sample_idx);
CaptionRecord* record = samples_.at(sample_idx).get();
LOG(INFO) << "caption: " << record->get_caption();
caption_map[sample_idx] = record->get_caption();
LOG(INFO) << "caption_id: " << record->get_caption_id()
<< " caption_text: " << record->get_caption_text();
caption_id_map[sample_idx] = record->get_caption_id();
caption_text_map[sample_idx] = record->get_caption_text();
output_pixels_map[sample_idx] = output_pixels;
attention_mask_map[sample_idx] = record->get_attention_mask_vector();
input_ids_map[sample_idx] = record->get_input_ids_vector();
std::string raw_output_filename = raw_output_dir_ + "/caption_id_" +
std::to_string(record->get_caption_id()) +
".rgb8";
dump_output_pixels(output_pixels, raw_output_filename);
return output_pixels;
} else {
return std::vector<uint8_t>();
Expand All @@ -124,7 +126,8 @@ float CocoGen::ComputeAccuracy() {
float total_score = 0.0f;
float total_samples = static_cast<float>(sample_ids_.size());
for (int sample_idx : sample_ids_) {
std::string caption = caption_map[sample_idx];
int caption_id = caption_id_map[sample_idx];
std::string caption_text = caption_text_map[sample_idx];
std::vector<int32_t> input_ids = input_ids_map[sample_idx];
std::vector<int32_t> attention_mask = attention_mask_map[sample_idx];
std::vector<uint8_t> output_pixels = output_pixels_map[sample_idx];
Expand All @@ -134,8 +137,8 @@ float CocoGen::ComputeAccuracy() {
}
float score =
score_predictor_.predict(attention_mask, input_ids, pixel_values);
LOG(INFO) << "sample_idx: " << sample_idx << " caption: " << caption
<< " score: " << score;
LOG(INFO) << "sample_idx: " << sample_idx << " caption_id: " << caption_id
<< " caption_text: " << caption_text << " score: " << score;
total_score += score;
}
float avg_score = total_score / total_samples;
Expand Down
3 changes: 2 additions & 1 deletion flutter/cpp/datasets/coco_gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class CocoGen : public Dataset {
std::set<int> sample_ids_;
bool isModelFound;
std::string raw_output_dir_;
std::unordered_map<int, std::string> caption_map;
std::unordered_map<int, int> caption_id_map;
std::unordered_map<int, std::string> caption_text_map;
std::unordered_map<int, std::vector<uint8_t>> output_pixels_map;
std::unordered_map<int, std::vector<int32_t>> attention_mask_map;
std::unordered_map<int, std::vector<int32_t>> input_ids_map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"source": [
"SAVED_MODEL_DIR = './clip_model'\n",
"TFLITE_MODEL_PATH = './clip_model.tflite'\n",
"MODEL_NAME = \"openai/clip-vit-base-patch32\""
"MODEL_NAME = \"openai/clip-vit-large-patch14\""
],
"metadata": {
"id": "eOxB3zL_33tq"
Expand Down
5 changes: 4 additions & 1 deletion flutter/cpp/datasets/coco_gen_utils/generate_tfrecords.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ def download_image(url, file_path):
print(f"Downloaded image to {file_path}")


def serialize_example(caption, input_ids, attention_mask, file_name, clip_score):
def serialize_example(caption_id, caption, input_ids, attention_mask, file_name, clip_score):
"""Creates a tf.train.Example message ready to be written to a file."""
feature = {
'caption_id': tf.train.Feature(int64_list=tf.train.Int64List(value=caption_id)),
'caption': tf.train.Feature(bytes_list=tf.train.BytesList(value=[caption.encode()])),
'input_ids': tf.train.Feature(int64_list=tf.train.Int64List(value=input_ids)),
'attention_mask': tf.train.Feature(int64_list=tf.train.Int64List(value=attention_mask)),
Expand Down Expand Up @@ -87,6 +88,7 @@ def main():
with tf.io.TFRecordWriter(args.output_tfrecord, options='ZLIB') as writer:
total = len(df)
for idx, row in df.iterrows():
caption_id = row['id']
caption = row['caption']
file_name = row['file_name']
coco_url = row['coco_url']
Expand All @@ -104,6 +106,7 @@ def main():
clip_score = outputs.logits_per_image.numpy().flatten().tolist()

example = serialize_example(
caption_id=[int(caption_id)],
caption=caption,
input_ids=input_ids,
attention_mask=attention_mask,
Expand Down
16 changes: 12 additions & 4 deletions flutter/cpp/datasets/coco_gen_utils/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ struct CaptionRecord {
tensorflow::Example example;
example.ParseFromString(record);

auto caption_id_list =
tensorflow::GetFeatureValues<int64_t>("caption_id", example);
caption_id =
std::vector<int32_t>(caption_id_list.begin(), caption_id_list.end())[0];

auto caption_list =
tensorflow::GetFeatureValues<string>("caption", example);
caption =
caption_text =
std::vector<std::string>(caption_list.begin(), caption_list.end());

auto input_id_list =
Expand All @@ -57,7 +62,8 @@ struct CaptionRecord {

void dump() {
std::cout << "CaptionRecord:\n";
std::cout << " caption: " << get_caption() << "\n";
std::cout << " caption_id: " << get_caption_id() << "\n";
std::cout << " caption_text: " << get_caption_text() << "\n";
std::cout << " input_ids: ";
for (size_t i = 0; i < input_ids.size(); ++i) {
std::cout << input_ids[i];
Expand All @@ -80,15 +86,17 @@ struct CaptionRecord {
std::cout << " clip_score: " << clip_score << "\n";
}

std::string get_caption() const { return caption[0]; }
int get_caption_id() const { return caption_id; }
std::string get_caption_text() const { return caption_text[0]; }
std::string get_filename() const { return filename[0]; }
int32_t* get_input_ids() { return input_ids.data(); }
int32_t* get_attention_mask() { return attention_mask.data(); }
std::vector<int32_t> get_input_ids_vector() { return input_ids; }
std::vector<int32_t> get_attention_mask_vector() { return attention_mask; }

private:
std::vector<std::string> caption;
int caption_id;
std::vector<std::string> caption_text;
std::vector<int32_t> input_ids;
std::vector<int32_t> attention_mask;
std::vector<std::string> filename;
Expand Down
4 changes: 2 additions & 2 deletions mobile_back_apple/dev-utils/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ tflite-run-sd:
bazel-bin/flutter/cpp/binary/main EXTERNAL stable_diffusion \
--mode=PerformanceOnly \
--output_dir="${REPO_ROOT_DIR}/output" \
--model_file="${REPO_ROOT_DIR}/mobile_back_apple/dev-resources/stable_diffusion/sd-models" \
--model_file="${REPO_ROOT_DIR}/mobile_back_apple/dev-resources/stable_diffusion/dynamic-sd-models" \
--lib_path="bazel-bin/mobile_back_tflite/cpp/backend_tflite/libtflitebackend.so" \
--input_tfrecord="${REPO_ROOT_DIR}/mobile_back_apple/dev-resources/stable_diffusion/coco_gen_full.tfrecord" \
--input_tfrecord="${REPO_ROOT_DIR}/mobile_back_apple/dev-resources/stable_diffusion/coco_gen_test.tfrecord" \
--input_clip_model="${REPO_ROOT_DIR}/mobile_back_apple/dev-resources/stable_diffusion/clip_model_512x512.tflite" \
--min_query_count=5

Expand Down