Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMDB Classifier #319

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion examples/text_classification/config_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# used for bert executor example
max_batch_tokens = 128

train_batch_size = 32
train_batch_size = 24
max_train_epoch = 5
display_steps = 50 # Print training loss every display_steps; -1 to disable

Expand Down
28 changes: 10 additions & 18 deletions examples/text_classification/download_imdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,19 @@
# 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 os
import sys
import subprocess
"""
Download IMDB dataset.
"""
from forte.data.data_utils import maybe_download


def main():
if not os.path.exists("data/IMDB_raw"):
subprocess.run("mkdir data/IMDB_raw", shell=True, check=True)
# pylint: disable=line-too-long
subprocess.run(
'wget -P data/IMDB_raw/ https://github.com/google-research/uda/blob/master/text/data/IMDB_raw/train_id_list.txt',
shell=True, check=True)
subprocess.run(
'wget -P data/IMDB_raw/ https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz',
shell=True, check=True)
subprocess.run(
'tar xzvf data/IMDB_raw/aclImdb_v1.tar.gz -C data/IMDB_raw/ && rm data/IMDB_raw/aclImdb_v1.tar.gz',
shell=True, check=True)
download_path = "data/IMDB_raw"
maybe_download(urls=[
"https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"],
path=download_path,
extract=True)


if __name__ == '__main__':
sys.exit(main())
main()
3 changes: 2 additions & 1 deletion examples/text_classification/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@

import os

from forte.models.imdb_text_classifier.model import IMDBClassifier
import config_data
import config_classifier

from forte.models.imdb_text_classifier.model import IMDBClassifier
Copy link
Member

Choose a reason for hiding this comment

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

In which PR can I find this classifier?

Copy link
Member

Choose a reason for hiding this comment

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

Is the classifier for IMDB only? if it is a general LSTM or CNN classifier we should consider renaming it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is in this PR forte/models/imdb_text_classifier. It is a BERT text classifier. The BERT model itself is not specific to IMDB but this PR contains preprocessing code specific to IMDB dataset to make it work.

Copy link
Member

Choose a reason for hiding this comment

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

Can you move the preprocessing out from the core model?



def main():
model = IMDBClassifier(config_data, config_classifier)
Copy link
Member

Choose a reason for hiding this comment

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

Why the model is responsible for prepare_data?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The model expects a pickle data format which is specific to the model.

Copy link
Member

Choose a reason for hiding this comment

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

It is not a good idea to put all these into the model. This fix the model so that it can only do one thing.

Expand Down
67 changes: 67 additions & 0 deletions examples/text_classification/preprocess_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2020 The Forte Authors. All Rights Reserved.
#
# 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.
"""Read all data in IMDB and merge them to a csv file."""
import os

from forte.data.caster import MultiPackBoxer
from forte.data.multi_pack import MultiPack
from forte.data.readers import LargeMovieReader
from forte.pipeline import Pipeline
from forte.utils.utils_io import maybe_create_dir
from ft.onto.base_ontology import Document


def main():
pipeline = Pipeline[MultiPack]()
reader = LargeMovieReader()
pipeline.set_reader(reader)
pipeline.add(MultiPackBoxer())

pipeline.initialize()

dataset_path = "data/IMDB_raw/aclImdb/"
input_file_path = {
"train": os.path.join(dataset_path, "train"),
"test": os.path.join(dataset_path, "test")
}
output_path = "data/IMDB/"
maybe_create_dir(output_path)
output_file_path = {
"train": os.path.join(output_path, "train.csv"),
"test": os.path.join(output_path, "test.csv")
}
set_labels = {
"train": ["pos", "neg", "unsup"],
"test": ["pos", "neg"],
}

for split in ["train", "test"]:
with open(output_file_path[split], "w", encoding="utf-8")\
as output_file:
output_file.write("\t".join(["content", "label", "id"]) + "\n")
for label in set_labels[split]:
data_packs = \
pipeline.process_dataset(
os.path.join(input_file_path[split], label))
for pack in data_packs:
example_id = pack.get_pack('default').pack_name
for pack_name in pack.pack_names:
p = pack.get_pack(pack_name)
for doc in p.get(Document):
output_file.write(
"\t".join([doc.text, label, example_id]) + "\n")


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion examples/text_classification/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
python download_imdb.py
python utils/imdb_format.py --raw_data_dir=data/IMDB_raw/aclImdb --train_id_path=data/IMDB_raw/train_id_list.txt --output_dir=data/IMDB
python preprocess_pipeline.py
python main.py
Copy link
Member

Choose a reason for hiding this comment

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

Add a new line at the end.

112 changes: 0 additions & 112 deletions examples/text_classification/utils/imdb_format.py

This file was deleted.

2 changes: 1 addition & 1 deletion forte/models/imdb_text_classifier/config_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# used for bert executor example
max_batch_tokens = 128

train_batch_size = 32
train_batch_size = 24
Copy link
Member

Choose a reason for hiding this comment

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

Why do we have two copies of config data?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I put it in the model directory as an example of the expected parameters in config_data. The user can simply copy this file if they want to use the model.

Copy link
Member

Choose a reason for hiding this comment

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

We should probably only keep one to reduce maintenance effort.

max_train_epoch = 5
display_steps = 50 # Print training loss every display_steps; -1 to disable

Expand Down
Loading